********************************************************************************
*PROJECT: Legislature Integration and Bipartisanship: A Natural Experiment in Iceland
*PURPOSE: Create exhibits for Journal of Politics		 								   
********************************************************************************

clear all
set maxvar 120000
set matsize 11000
set more off
set scheme plotplainblind // s1color/plottig/plotplainblind
timer clear
timer on 99
pause on

local cosponfolder = "$path_pch/Data/analysis/Co-Sponsorship"
local votingfolderraw = "$path_pch/Data/raw/Voting"
local votingfolder = "$path_pch/Data/analysis/Voting"
local votingintfolder = "$path_pch/Data/intermediate/Voting"
local votingsimfolder = "$path_pch/Data/intermediate/Simulated"
local tablefolder = "$path_pch/Output"
local graphfolder = "$path_pch/Output/Figures"
tempfile datamod calendar regSess regSess2


// main switches
local all			1
local hetero		"party" 			// main dimension of heterogeneity: party vs. gender vs. coalition vs. experience
local Hetero		proper("`hetero'")
local reweight		0 					// whether to reweight or not (Gerber-Green 2012)
local RI			1 					// whether to do randomization inference or not
local runsRI		1000 				// number of randomization inference runs (max. 1000)

// these switches stay unchanged for analysis for the paper
local dropspeaker	1 					// whether drop MP-pairs containing speaker
local dropspecial	1 					// whether drop special sessions
local dropshort		1 					// whether drop 1 short session (147)
local fr			115 				// starting session
local to			148 				// ending session


// table switches
if `all'==1 {
	local dyadVote				1 // Table 1, A11 (set reweight local to 1 above), A13 (set hetero local to "coalition" above)
	local dyadVoteContest		1 // Table 2, A12 (set reweight local to 1 above), A14 (set hetero local to "coalition" above)
	local dyadVoteCornerDiff	1 // Table A6
	local mpVote				1 // Table 3, A22 (set reweight local to 1 above, and hetero to "party"), A27 (set hetero local to "coalition" above)
	local dyadCospon			1 // Table 4
	local dyadCosponCornerDiff	1 // Table A31
	local mpCospon				1 // Table 5, A32 (set reweight local to 1 above), A34 (set hetero local to "coalition" above)
	local dyadVoteYesNo			1 // Table A8
	local mpVoteOneTwo			1 // Table A28, A29 (set hetero local to "coalition" above)
	local mpVoteContest			1 // Table A26
	local mpCosponOneTwo		1 // Table A33
	local dyadVoteRobust		1 // Table A9
	local dyadVoteContestRobust	1 // Table A10
	local dyadBalance			1 // Table A1, A3 (set hetero local to "coalition" above)
	local mpBalance				1 // Table A2, A4 (set hetero local to "coalition" above)
	local dyadTrends			1 // Figure 3
	local mpTrends				1 // Figure 1
	local voteCalendar			1 // Figure A3
	local dyadVoteCrisis		1 // Table A21
	local dyadVoteYesNoAbs		1 // Table A7
	local mpCompound			1 // Table A35 (set hetero local to "coalition" above)
	local billsPassed			1 // Figure A1
	local dyadSUTVA				1 // Table A5
	local billType				1 // Table A15
	local mpVoteExper			1 // Table A23
	local dyadVotePartyDist		1 // Table A19
	local dyadVotePartyTerc		1 // Table A20
	local dyadmpSims			1 // Table C2 (note: need to change pcue local below to the different values used in the Table)
	local dyadmpSimsTab			1 // Table C2
	local billTopic				1 // Table A16
	local mpVoteAltOutcome		1 // Table A25
	local mpAbsentAbstain		1 // Table A30
	local dyadVoteBusy			1 // Table A17
	local dyadVoteGenderExper	1 // Table A18 (set hetero local to "gender" above), A24 (set hetero local to "experience" above)
	
}
else if `all'==0 {
	local dyadVote				1 // Table 1, A11 (set reweight local to 1 above), A13 (set hetero local to "coalition" above)
	local dyadVoteContest		0 // Table 2, A12 (set reweight local to 1 above), A14 (set hetero local to "coalition" above)
	local dyadVoteCornerDiff	0 // Table A6
	local mpVote				0 // Table 3, A22 (set reweight local to 1 above, and hetero to "party"), A27 (set hetero local to "coalition" above)
	local dyadCospon			0 // Table 4
	local dyadCosponCornerDiff	0 // Table A31
	local mpCospon				0 // Table 5, A32 (set reweight local to 1 above), A34 (set hetero local to "coalition" above)
	local dyadVoteYesNo			0 // Table A8
	local mpVoteOneTwo			0 // Table A28, A29 (set hetero local to "coalition" above)
	local mpVoteContest			0 // Table A26
	local mpCosponOneTwo		0 // Table A33
	local dyadVoteRobust		0 // Table A9
	local dyadVoteContestRobust	0 // Table A10
	local dyadBalance			0 // Table A1, A3 (set hetero local to "coalition" above)
	local mpBalance				0 // Table A2, A4 (set hetero local to "coalition" above)
	local dyadTrends			0 // Figure 3
	local mpTrends				0 // Figure 1
	local voteCalendar			0 // Figure A3
	local dyadVoteCrisis		0 // Table A21
	local dyadVoteYesNoAbs		0 // Table A7
	local mpCompound			0 // Table A35 (set hetero local to "coalition" above)
	local billsPassed			0 // Figure A1
	local dyadSUTVA				0 // Table A5
	local billType				0 // Table A15
	local mpVoteExper			0 // Table A23
	local dyadVotePartyDist		0 // Table A19
	local dyadVotePartyTerc		0 // Table A20
	local dyadmpSims			0 // Table C2 (note: need to change pcue local below to the different values used in the Table)
	local dyadmpSimsTab			0 // Table C2
	local billTopic				0 // Table A16
	local mpVoteAltOutcome		0 // Table A25
	local mpAbsentAbstain		0 // Table A30
	local dyadVoteBusy			0 // Table A17
	local dyadVoteGenderExper	0 // Table A18 (set hetero local to "gender" above), A24 (set hetero local to "experience" above)

}
if 1==1 { // Load and merge/modify data
	
	cd "`votingfolder'" 
	use dyads_voting, clear
	keep session_id MP_id MP_id_d samevote?c* votediff?c* voteSim?c* samevotePresent* samePresentAbsent*
	save `datamod'

	cd "`cosponfolder'"
	use dyads_with_cospon, clear
	merge 1:1 session_id MP_id MP_id_d using `datamod', assert(3) nogen
	if `dropspeaker'==1 {
		drop if speaker_in_pair==1
	}
	if `dropspecial'==1 {
		drop if specialSession==1
	}
	if `dropshort'==1 {
		drop if shortSession==1
	}	
	keep if session_id >= `fr' & session_id <= `to'
	drop p*1st c*1st r*1st d*1st i*1st o*1st p*2nd c*2nd r*2nd d*2nd i*2nd o*2nd
	
	if `runsRI'<1000 {
		local j=`runsRI'+1
		
		forv i=`j'/1000 {
			cap drop *RI`i'*
		}
	}
	save `datamod', replace
}
if 1==1 { // Labels
	// Outcome labels
	local noncompliancealtMP = "Party Non-Compliance is the proportion of times the MP votes differently from the modal vote in their party in a given session."
	local noncomplianceMP = "Leader Non-Compliance is the proportion of times the MP votes differently from their party leader in a given session."
	local absenceMP = "Absent is the proportion of times the MP was absent for a vote in a given session."	
	local abstainMP = "Abstain is the proportion of times the MP abstained from voting in a given session."		
	local differenceMP = "Leader Diff. is the average vote difference score between the MP and their party leader."	
	local rebelWeakMP = "Rebel Rate is the proportion of times the MP voted yes/abstain (no/abstain) when their party leader voted no (yes) in a given session."
	local rawnumbMP = "Number is the total number of co-sponsorship links between the MP and any other-party MP in a given session."
	local ihsAll = "IHS is the inverse hyperbolic sine transformation of Number."

	local compliance = "Compliance is the proportion of times the two MPs in a pair vote the same way in a given session."
	local similarity = "Similarity is the average vote similarity between the two MPs in a pair."
	local rawnumb = "Number is the total number of co-sponsorship links between the two MPs in a pair in a given session."
	local yesyes = "Yes-Yes/No-No is the proportion of times the two MPs in a pair both vote yes or both vote no in a given session."
	local yynnaa = "Yes-Yes/No-No/Abstain-Abstain is the proportion of times the two MPs in a pair both vote yes, or both vote no, or both abstain in a given session."
	local presabs = "Both Present/Absent is the proportion of times the two MPs in a pair are either both absent or both present for a vote in a given session."
	local rawresrep = "Raw Number is the total number of links through co-sponsorship of bills/resolutions/reports between the two MPs in a pair in a given session."

	local comp3 = "Compliance 3-Cat is the proportion of times the two MPs in a pair vote the same way in a given session, with absence considered equivalent to abstention."
	local sim3 = "Similarity 3-Cat is the average vote similarity between the two MPs in a pair, with absence considered equivalent to abstention."
	local simRec = "Similarity Recode is the pair-level average vote similarity, with absence coded as closer to a no vote than abstention."
	
	// RHS labels
	local propothMP = "Proportion Other-`Hetero' Neighbor is the proportion of left-right seating neighbors from a different `hetero'."
	local neighbor = "Neighbor is a dummy variable equal to one if the MPs in the pair are randomly assigned to sit next to each other during that session."
	local samehet = "Same `Hetero' is equal to one if both MPs in the pair are in the same `hetero' for that session."
	local diffPair = "Diff. Coalition/Gender are dummy variables equal to one of the two MPs in the pair are in different coalitions/genders. Age/Tenure Diff. are the absolute difference in age/years in parliament between the pair of MPs."
	local corner = "Corner is equal to one if at least one MP in pair has only one seating neighbor. Middle is equal to one minus Corner."
	local strataDef = "Strata FE are dummy variables for whether both MPs in a pair were pre-assigned seats, one MP in a pair was pre-assigned a seat, or neither MP in a pair was pre-assigned a seat."
	local strataDefMP = "Strata FE is a dummy variable for whether MP was pre-assigned a seat."
	
	// Other labels
	local contested "Below 50th/25th votes are votes in which the share of MPs voting the modal vote is less than the median/25th percentile among all votes." 	
	local plab = "*** p<0.01, ** p<0.05, * p<0.1."
	local riCurly = "Randomization inference p-values (`runsRI' draws) are in curly brackets."
	local qlab = "Sharpened q-values (Anderson 2008) for non-placebo tests are in <>."
	local eachPanelFour = "Each panel shows the estimates from four linear regressions."
	local dyadSEp = "Dyadic-robust standard errors are in parentheses and dyadic-robust p-values are in square brackets."
	local dyadSquare = "Dyadic-robust p-values are in square brackets."
	local mpSEp = "MP-clustered standard errors are in parentheses and p-values are in square brackets."
	local mpSquare = "MP-clustered p-values are in square brackets."
	local specShortDrop = "Special sessions and a short session (2017) are excluded."
	local leadLagDrop = "For lead and lag specifications, sessions are also dropped where lead/lag would be a special/short session or a session in a different parliamentary term."
	local outcomeMeanA = "Outcome Mean and Standard Deviation are for the sample included in the Panel A regressions."
	local diffPartyOnly = "Regressions include different-party dyads only, with neither MP pre-assigned seats."
	if "`hetero'"=="party" {
		local bipart = "(proximity effect on bipartisanship)"
	}
	else {
		local bipart = ""
	}
	if `reweight'==1 {
		local wgt "[aw=invProb]"
		local wgtlab "Wgt"
		local wgttabnote "Observations are weighted by the block-level inverse probability of treatment assignment, following Gerber and Green (2012). "
	}
	else {
		local wgt ""
		local wgtlab ""
		local wgttabnote ""
	}

}
if `dyadVote'==1 { // Dyadic voting results
	
	timer on 1
	forv k = 1/3 { 		// Time period?
		local time: word `k' of "Lag" "" "Lead"

		forv r = 1/2 {	// Type of voting/cosponsorship measure?
			local measure: word `r' of "samevote4c" "voteSim4c"
				
				dis "`measure'`time'"
				use `datamod', clear
								
				keep session_id samevote?c* votediff?c* voteSim?c* *RI* minMP maxMP pairStrata samecoalition sameparty ///
				samegender specialSession shortSession firstSession lastSession leadShortIncomplete	party_id party_id_d	///
				sessionXpairStrataXpartyPair
				
				if `k'==1 {
					drop if firstSession==1 
				}
				else if `k'==3 {
					drop if lastSession==1
					drop if leadShortIncomplete==1
				}
				
				/* Calculate weights */
				if `reweight'==1 {
					bys sessionXpairStrataXpartyPair: egen probTreat = mean(neighborRI0)
					g invProb = 1/probTreat if neighborRI0==1
					replace invProb = 1/(1-probTreat) if neighborRI0==0
				}
				
				/* Neighborhood effect */
				
				// residualize
				qui areg `measure'`time' if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				local numN = e(N)
				local numDummy = e(df_a) 
				predict yresid, residuals
				qui areg neighborRI0 if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				local numN2 = e(N)
				predict xresid, residuals
				assert `numN' == `numN2'
				
				// run regression
				xi: regdyad2 yresid xresid if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d) `wgt', ///
					dyads(minMP maxMP)
				est sto M`time'`r'
				local obs`time'`r' = e(N)
				qui su `measure'`time' if e(sample)==1
				local mean`time'`r' = string(r(mean),"%9.2g")
				local sd`time'`r' = string(r(sd),"%9.2g")
				local sd`time'`r'raw = `r(sd)'
				local numReg = 1
				
				// prepare coefficient, standard error, p-value for export
				local coef_`time'`r'_av = _b[xresid]
				local se_`time'`r'_av = string(_se[xresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1)),"%9.2g")
				local t_df = `numN'-`numReg'-`numDummy'-1
				local pval_`time'`r'_av = string(ttail(`t_df',abs(`coef_`time'`r'_av'/`se_`time'`r'_av'))*2,"%9.2g")					
				drop yresid xresid	
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme = 0
					forv i=1/`runsRI' {
						
						/* Re-calculate weights */
						if `reweight'==1 {
							drop probTreat invProb
							bys sessionXpairStrataXpartyPair: egen probTreat = mean(neighborRI`i')
							g invProb = 1/probTreat if neighborRI`i'==1
							replace invProb = 1/(1-probTreat) if neighborRI`i'==0
						}
						
						areg `measure'`time' neighborRI`i' `wgt', absorb(sessionXpairStrataXpartyPair)
						local absCoef = abs(_b[neighborRI`i'])
						local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`time'`r'_av')) // + 1 every time see more extreme beta
					}
					local pval_`time'`r'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
				}
				else {
					local pval_`time'`r'_RI = 999
				}	
				
				
				/* Re-calculate weights */
				if `reweight'==1 {
					drop probTreat invProb
					bys sessionXpairStrataXpartyPair: egen probTreat = mean(neighborRI0)
					g invProb = 1/probTreat if neighborRI0==1
					replace invProb = 1/(1-probTreat) if neighborRI0==0
				}				
				
				/* Hetero: party ingroup and outgroup effect */
				
				// residualize			
				qui areg `measure'`time' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') ///
					& !mi(`measure'`time') & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair)
				local numN = e(N)
				local numDummy = e(df_a) 
				predict yresid, residuals
				
				qui areg neighborRI0Xsame`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') ///
					& !mi(`measure'`time') & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				local numN2 = e(N)
				predict xinresid, residuals
				assert `numN' == `numN2'
				qui areg neighborRI0Xdiff`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') ///
					& !mi(`measure'`time') & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				predict xoutresid, residuals
				
				// run regression
				xi: regdyad2 yresid xinresid xoutresid if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') ///
					& !mi(`measure'`time') & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d) `wgt', dyads(minMP maxMP)
				est sto Het`time'`r'
				local obsHet`time'`r' = e(N)
				local numReg = 2
				matrix varMatrix = e(V)
				
				// prepare coefficient, standard error, p-value for export					
				local coef_`time'`r'_in = string(_b[xinresid],"%9.2g")
				local coef_`time'`r'_out = string(_b[xoutresid],"%9.2g")
				local se_`time'`r'_in = string(_se[xinresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1)),"%9.2g")
				local se_`time'`r'_out = string(_se[xoutresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1)),"%9.2g")
				local t_df = `numN'-`numReg'-`numDummy'-1
				local pval_`time'`r'_in =  string(ttail(`t_df',abs(`coef_`time'`r'_in'/`se_`time'`r'_in'))*2,"%9.2g")
				local pval_`time'`r'_out =  string(ttail(`t_df',abs(`coef_`time'`r'_out'/`se_`time'`r'_out'))*2,"%9.2g")
				
				// pval calculation for the difference
				local diff = `coef_`time'`r'_in' - `coef_`time'`r'_out'
				local se_`time'`r'_diff = sqrt(varMatrix[1,1]+varMatrix[2,2]-2*varMatrix[2,1])
				local pval_`time'`r'_diff =  string(ttail(`t_df',abs(`diff'/`se_`time'`r'_diff'))*2,"%9.2g")
				drop yresid xinresid xoutresid
				
				// diff-party point estimate and 95% confidence interval for figure
				if `k'==2 {
					local core_`r'_out = `coef_`r'_out'/`sd`r'raw'
					local corel_`r'_out = (`coef_`r'_out' - invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
					local coreu_`r'_out = (`coef_`r'_out' + invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
				}
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme_in = 0
					local Nbeta_extreme_out = 0
					local Nbeta_extreme_diff = 0
					forv i=1/`runsRI' {
						
						/* Re-calculate weights */
						if `reweight'==1 {
							drop probTreat invProb
							bys sessionXpairStrataXpartyPair: egen probTreat = mean(neighborRI`i')
							g invProb = 1/probTreat if neighborRI`i'==1
							replace invProb = 1/(1-probTreat) if neighborRI`i'==0
						}
						
						areg `measure'`time' neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero' `wgt', absorb(sessionXpairStrataXpartyPair)
												
						local absCoefIn = abs(_b[neighborRI`i'Xsame`hetero'])
						local absCoefOut = abs(_b[neighborRI`i'Xdiff`hetero'])
						local Nbeta_extreme_in = `Nbeta_extreme_in' + (`absCoefIn' >= abs(`coef_`time'`r'_in')) // + 1 every time see more extreme beta
						local Nbeta_extreme_out = `Nbeta_extreme_out' + (`absCoefOut' >= abs(`coef_`time'`r'_out')) 
					
						// for difference between in vs out p-value we need separate regressions, using "modified" outcome
						g `measure'`time'_m = `measure'`time'
						replace `measure'`time'_m = `measure'`time'_m + `coef_`time'`r'_av' if neighborRI`i' == 1 & neighborRI0 == 0
						replace `measure'`time'_m = `measure'`time'_m - `coef_`time'`r'_av' if neighborRI`i' == 0 & neighborRI0 == 1	
						
						// run regression with modified outcome
						areg `measure'`time'_m neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero' `wgt', absorb(sessionXpairStrataXpartyPair)
						local absCoefDiff = abs(_b[neighborRI`i'Xsame`hetero']-_b[neighborRI`i'Xdiff`hetero'])
						local Nbeta_extreme_diff = `Nbeta_extreme_diff' + (`absCoefDiff' >= abs(`diff')) 
						drop `measure'`time'_m
					}
					local pval_`time'`r'_RIin = string(`Nbeta_extreme_in'/`runsRI',"%9.2g")
					local pval_`time'`r'_RIout = string(`Nbeta_extreme_out'/`runsRI',"%9.2g")	
					local pval_`time'`r'_RIdiff = string(`Nbeta_extreme_diff'/`runsRI',"%9.2g")	
				}
				else {
					local pval_`time'`r'_RIin = 999
					local pval_`time'`r'_RIout = 999	
					local pval_`time'`r'_RIdiff = 999										
				}					
				
				// get stars for all pvals
				foreach y in av in out diff RI RIin RIout RIdiff {
					local pstar_`time'`r'_`y' = "" // reset p-value
					if `pval_`time'`r'_`y''<0.10 & `pval_`time'`r'_`y''>=0.05 local pstar_`time'`r'_`y' = "*"
					if `pval_`time'`r'_`y''<0.05 & `pval_`time'`r'_`y''>=0.01 local pstar_`time'`r'_`y' = "**"
					if `pval_`time'`r'_`y''<0.01 local pstar_`time'`r'_`y' = "***"
				}
				
				// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
				foreach y in av in out diff {
					if `pval_`time'`r'_`y''<0.001 local pval_`time'`r'_`y' = "<0.001" 
				}					
		}
	}
		
	// Output Main Effects Table (appendix?)
	estout M1 M2 MLead1 MLead2 MLag1 MLag2 using "`tablefolder'//pairVoteA`wgtlab'.tex", style(tex) replace ///
	keep(xresid) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(xresid "Neighbor")

	local tex "\input{pairVoteA`wgtlab'} \vspace{-1.2em} \\"
	local tex "`tex' & (`se_1_av') & (`se_2_av') & (`se_Lead1_av') & (`se_Lead2_av') & (`se_Lag1_av') & (`se_Lag2_av') \\"
	local tex "`tex' & [`pval_1_av']`pstar_1_av' & [`pval_2_av']`pstar_2_av' & [`pval_Lead1_av']`pstar_Lead1_av' & [`pval_Lead2_av']`pstar_Lead2_av' & [`pval_Lag1_av']`pstar_Lag1_av' & [`pval_Lag2_av']`pstar_Lag2_av' \\"
	local tex "`tex' & \{`pval_1_RI'\}`pstar_1_RI' & \{`pval_2_RI'\}`pstar_2_RI' &  \{`pval_Lead1_RI'\}`pstar_Lead1_RI' & \{`pval_Lead2_RI'\}`pstar_Lead2_RI' & \{`pval_Lag1_RI'\}`pstar_Lag1_RI' & \{`pval_Lag2_RI'\}`pstar_Lag2_RI' \\ \\[-5pt] \hline"	
	
	local tex "`tex' Observations & `obs1' & `obs2' & `obsLead1' & `obsLead2' & `obsLag1' & `obsLag2' \\"
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `meanLead1' & `meanLead2' & `meanLag1' & `meanLag2' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sdLead1' & `sdLead2' & `sdLag1' & `sdLag2' \\ \toprule" 
	
	local tex "`tex' \multicolumn{7}{p{19.3cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `wgttabnote'`compliance' `similarity'"
	local tex "`tex' `neighbor'"
	local tex "`tex' `dyadSEp' `riCurly'" 
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `plab'}"	
	local tex "`tex' \\ \end{tabular} }"
	
	esttab M1 M2 MLead1 MLead2 MLag1 MLag2 using "`tablefolder'//pairVote`wgtlab'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("\specialcell{Contemporaneous\\Effect (t)}" "\specialcell{One Year\\Later (t+1)}" ///
			"\specialcell{Previous Year\\(Placebo) (t-1)}", pattern(1 0 1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Compliance\\(1)}" "\specialcell{Similarity\\(2)}" ///
	"\specialcell{Compliance\\(3)}" "\specialcell{Similarity\\(4)}" ///	
	"\specialcell{Compliance\\(5)}" "\specialcell{Similarity\\(6)}")	
	
	// Get sharpened q-values (Anderson 2008)	
	g float pval=.
	local i=1
	foreach x in pval_1_out pval_2_out pval_Lead1_out pval_Lead2_out ///
		pval_1_in pval_2_in pval_Lead1_in pval_Lead2_in {
		
		replace pval=``x'' in `i'
		local i=`i'+1
	}
	keep pval
	drop if mi(pval)
		
	run "$path_pch/Code/analysis/fdr_sharpened_qvalues.do"
	
	local i=1
	foreach x in qval_1_out qval_2_out qval_Lead1_out qval_Lead2_out ///
		qval_1_in qval_2_in qval_Lead1_in qval_Lead2_in {
		
		local `x' = string(qval,"%9.2g") in `i'
		if qval<0.001 in `i' local `x' = "<0.001" 
		
		local i=`i'+1
	}	
	
	// get stars for qvals
	local i=1
	foreach y in qstar_1_out qstar_2_out qstar_Lead1_out qstar_Lead2_out qstar_1_in qstar_2_in qstar_Lead1_in qstar_Lead2_in {
		local `y' = "" // reset q-star
		local j = qval in `i' // raw p-value
		
		if `j'<0.10 & `j'>=0.05 local `y' = "*"
		if `j'<0.05 & `j'>=0.01 local `y' = "**"
		if `j'<0.01 local `y' = "***"
				
		local i=`i'+1
	}

	// Output Heterogeneous Effects Table
	local tex "Neighbor $\times$ Different `Hetero' & `coef_1_out' & `coef_2_out' & `coef_Lead1_out' & `coef_Lead2_out' & `coef_Lag1_out' & `coef_Lag2_out' \\"
	local tex "`tex' `bipart' & (`se_1_out') & (`se_2_out') & (`se_Lead1_out') & (`se_Lead2_out') & (`se_Lag1_out') & (`se_Lag2_out') \\"	
	local tex "`tex' & [`pval_1_out']`pstar_1_out' & [`pval_2_out']`pstar_2_out' & [`pval_Lead1_out']`pstar_Lead1_out' & [`pval_Lead2_out']`pstar_Lead2_out' & [`pval_Lag1_out']`pstar_Lag1_out' & [`pval_Lag2_out']`pstar_Lag2_out' \\"
	local tex "`tex' & \{`pval_1_RIout'\}`pstar_1_RIout' & \{`pval_2_RIout'\}`pstar_2_RIout' & \{`pval_Lead1_RIout'\}`pstar_Lead1_RIout' & \{`pval_Lead2_RIout'\}`pstar_Lead2_RIout' & \{`pval_Lag1_RIout'\}`pstar_Lag1_RIout' & \{`pval_Lag2_RIout'\}`pstar_Lag2_RIout' \\"
	local tex "`tex' & <`qval_1_out'>`qstar_1_out' & <`qval_2_out'>`qstar_2_out' & <`qval_Lead1_out'>`qstar_Lead1_out' & <`qval_Lead2_out'>`qstar_Lead2_out' & & \\[5pt]"		
	
	local tex "`tex' Neighbor $\times$ Same `Hetero' & `coef_1_in' & `coef_2_in' & `coef_Lead1_in' & `coef_Lead2_in'  & `coef_Lag1_in' & `coef_Lag2_in' \\"
	local tex "`tex' & (`se_1_in') & (`se_2_in') & (`se_Lead1_in') & (`se_Lead2_in') & (`se_Lag1_in') & (`se_Lag2_in') \\"		
	local tex "`tex'  & [`pval_1_in']`pstar_1_in' & [`pval_2_in']`pstar_2_in' & [`pval_Lead1_in']`pstar_Lead1_in' & [`pval_Lead2_in']`pstar_Lead2_in' & [`pval_Lag1_in']`pstar_Lag1_in' & [`pval_Lag2_in']`pstar_Lag2_in' \\"
	local tex "`tex' & \{`pval_1_RIin'\}`pstar_1_RIin' & \{`pval_2_RIin'\}`pstar_2_RIin' & \{`pval_Lead1_RIin'\}`pstar_Lead1_RIin' & \{`pval_Lead2_RIin'\}`pstar_Lead2_RIin' & \{`pval_Lag1_RIin'\}`pstar_Lag1_RIin' & \{`pval_Lag2_RIin'\}`pstar_Lag2_RIin' \\"
	local tex "`tex' & <`qval_1_in'>`qstar_1_in' & <`qval_2_in'>`qstar_2_in' & <`qval_Lead1_in'>`qstar_Lead1_in' & <`qval_Lead2_in'>`qstar_Lead2_in' & & \\[5pt]"		
		
	local tex "`tex' Same = Different & [`pval_1_diff']`pstar_1_diff' & [`pval_2_diff']`pstar_2_diff' & [`pval_Lead1_diff']`pstar_Lead1_diff' & [`pval_Lead2_diff']`pstar_Lead2_diff' & [`pval_Lag1_diff']`pstar_Lag1_diff' & [`pval_Lag2_diff']`pstar_Lag2_diff' \\"
	local tex "`tex' & \{`pval_1_RIdiff'\}`pstar_1_RIdiff' & \{`pval_2_RIdiff'\}`pstar_2_RIdiff' & \{`pval_Lead1_RIdiff'\}`pstar_Lead1_RIdiff' & \{`pval_Lead2_RIdiff'\}`pstar_Lead2_RIdiff' & \{`pval_Lag1_RIdiff'\}`pstar_Lag1_RIdiff' & \{`pval_Lag2_RIdiff'\}`pstar_Lag2_RIdiff' \\ \\[-5pt] \hline"				
	local tex "`tex' Observations & `obsHet1' & `obsHet2' & `obsHetLead1' & `obsHetLead2' & `obsHetLag1' & `obsHetLag2' \\" 
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `meanLead1' & `meanLead2' & `meanLag1' & `meanLag2' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sdLead1' & `sdLead2' & `sdLag1' & `sdLag2' \\ \toprule" 
	
	local tex "`tex' \multicolumn{7}{p{20cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `wgttabnote'`compliance' `similarity'"
	local tex "`tex' `neighbor' `samehet'"
	local tex "`tex' `dyadSEp' `riCurly' `qlab'"
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `plab'}"
	local tex "`tex' \\ \end{tabular} }"	
	
	esttab Het1 Het2 HetLead1 HetLead2 HetLag1 HetLag2 using "`tablefolder'//pairVote`Hetero'`wgtlab'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("\specialcell{Contemporaneous\\Effect (t)}" "\specialcell{One Year\\Later (t+1)}" ///
			"\specialcell{Previous Year\\(Placebo) (t-1)}", pattern(1 0 1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Compliance\\(1)}" "\specialcell{Similarity\\(2)}" ///
	"\specialcell{Compliance\\(3)}" "\specialcell{Similarity\\(4)}" ///	
	"\specialcell{Compliance\\(5)}" "\specialcell{Similarity\\(6)}")	
	
	timer off 1
	disp "DONE dyadVote"
	timer list 1
	
	beep
}
if `dyadVoteContest'==1 { // Dyadic voting results (contested)

	timer on 2
	forv k = 1/3 { 		// Time period?
		local time: word `k' of "Lag" "" "Lead"

		forv r = 1/4 {	// Type of voting/cosponsorship measure?
			local measure: word `r' of "samevote4c_VS" "voteSim4c_VS" "samevote4c_VVS" "voteSim4c_VVS"
				
				dis "`measure'`time'"
				use `datamod', clear
				keep session_id *coany* samevote?c* votediff?c* voteSim?c* *RI* minMP maxMP pairStrata samecoalition sameparty ///
				samegender specialSession shortSession firstSession lastSession leadShortIncomplete	sessionXpairStrataXpartyPair ///
				party_id party_id_d
				
				if `k'==1 {
					drop if firstSession==1 
				}
				else if `k'==3 {
					drop if lastSession==1
					drop if leadShortIncomplete==1
				}
				
				/* Calculate weights */
				if `reweight'==1 {
					bys sessionXpairStrataXpartyPair: egen probTreat = mean(neighborRI0)
					g invProb = 1/probTreat if neighborRI0==1
					replace invProb = 1/(1-probTreat) if neighborRI0==0
				}

				
				/* Neighborhood effect */
				
				// residualize
				qui areg `measure'`time' if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				local numN = e(N)
				local numDummy = e(df_a) 
				predict yresid, residuals
				qui areg neighborRI0 if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				local numN2 = e(N)
				predict xresid, residuals
				assert `numN' == `numN2'
				
				// run regression
				xi: regdyad2 yresid xresid if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d) `wgt', ///
					dyads(minMP maxMP)
				est sto M`time'`r'
				local obs`time'`r' = e(N)
				qui su `measure'`time' if e(sample)==1
				local mean`time'`r' = string(r(mean),"%9.2g")
				local sd`time'`r' = string(r(sd),"%9.2g")
				local sd`time'`r'raw = `r(sd)'
				local numReg = 1
				
				// prepare coefficient, standard error, p-value for export
				local coef_`time'`r'_av = _b[xresid]
				local se_`time'`r'_av = string(_se[xresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1)),"%9.2g")
				local t_df = `numN'-`numReg'-`numDummy'-1
				local pval_`time'`r'_av = string(ttail(`t_df',abs(`coef_`time'`r'_av'/`se_`time'`r'_av'))*2,"%9.2g")					
				drop yresid xresid	
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme = 0
					forv i=1/`runsRI' {
						
						/* Re-calculate weights */
						if `reweight'==1 {
							drop probTreat invProb
							bys sessionXpairStrataXpartyPair: egen probTreat = mean(neighborRI`i')
							g invProb = 1/probTreat if neighborRI`i'==1
							replace invProb = 1/(1-probTreat) if neighborRI`i'==0
						}
						
						areg `measure'`time' neighborRI`i' `wgt', absorb(sessionXpairStrataXpartyPair)
						local absCoef = abs(_b[neighborRI`i'])
						local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`time'`r'_av')) // + 1 every time see more extreme beta
					}
					local pval_`time'`r'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
				}
				else {
					local pval_`time'`r'_RI = 999
				}	
		
				/* Re-calculate weights */
				if `reweight'==1 {
					drop probTreat invProb
					bys sessionXpairStrataXpartyPair: egen probTreat = mean(neighborRI0)
					g invProb = 1/probTreat if neighborRI0==1
					replace invProb = 1/(1-probTreat) if neighborRI0==0
				}				
				
				/* Hetero: party ingroup and outgroup effect */
				
				// residualize
				qui areg `measure'`time' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure'`time') ///
					& !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				local numN = e(N)
				local numDummy = e(df_a) 
				predict yresid, residuals
				qui areg neighborRI0Xsame`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure'`time') ///
					& !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				local numN2 = e(N)
				predict xinresid, residuals
				assert `numN' == `numN2'
				qui areg neighborRI0Xdiff`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure'`time') & ///
					!mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				predict xoutresid, residuals
				
				// run regression
				xi: regdyad2 yresid xinresid xoutresid if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & ///
					!mi(`measure'`time') & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d) `wgt', dyads(minMP maxMP)
				est sto Het`time'`r'
				local obsHet`time'`r' = e(N)
				local numReg = 2
				matrix varMatrix = e(V)
				
				// prepare coefficient, standard error, p-value for export					
				local coef_`time'`r'_in = string(_b[xinresid],"%9.2g")
				local coef_`time'`r'_out = string(_b[xoutresid],"%9.2g")
				local se_`time'`r'_in = _se[xinresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
				local se_`time'`r'_out = _se[xoutresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
				local t_df = `numN'-`numReg'-`numDummy'-1
				local pval_`time'`r'_in_raw = ttail(`t_df',abs(`coef_`time'`r'_in'/`se_`time'`r'_in'))*2
				local pval_`time'`r'_out_raw = ttail(`t_df',abs(`coef_`time'`r'_out'/`se_`time'`r'_out'))*2
				local pval_`time'`r'_in =  string(ttail(`t_df',abs(`coef_`time'`r'_in'/`se_`time'`r'_in'))*2,"%9.2g")
				local pval_`time'`r'_out =  string(ttail(`t_df',abs(`coef_`time'`r'_out'/`se_`time'`r'_out'))*2,"%9.2g")
				
				// pval calculation for the difference
				local diff = `coef_`time'`r'_in' - `coef_`time'`r'_out'
				local se_`time'`r'_diff = sqrt(varMatrix[1,1]+varMatrix[2,2]-2*varMatrix[2,1])
				local pval_`time'`r'_diff =  string(ttail(`t_df',abs(`diff'/`se_`time'`r'_diff'))*2,"%9.2g")
				drop yresid xinresid xoutresid
				
				// diff-party point estimate and 95% confidence interval for figure
				if `k'==2 {
					local vote_`r'_out = `coef_`r'_out'/`sd`r'raw'
					local votel_`r'_out = (`coef_`r'_out' - invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
					local voteu_`r'_out = (`coef_`r'_out' + invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
				}
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme_in = 0
					local Nbeta_extreme_out = 0
					local Nbeta_extreme_diff = 0
					forv i=1/`runsRI' {
						
						/* Re-calculate weights */
						if `reweight'==1 {
							drop probTreat invProb
							bys sessionXpairStrataXpartyPair: egen probTreat = mean(neighborRI`i')
							g invProb = 1/probTreat if neighborRI`i'==1
							replace invProb = 1/(1-probTreat) if neighborRI`i'==0
						}
						
						areg `measure'`time' neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero' `wgt', absorb(sessionXpairStrataXpartyPair)
						local absCoefIn = abs(_b[neighborRI`i'Xsame`hetero'])
						local absCoefOut = abs(_b[neighborRI`i'Xdiff`hetero'])
						local Nbeta_extreme_in = `Nbeta_extreme_in' + (`absCoefIn' >= abs(`coef_`time'`r'_in')) // + 1 every time see more extreme beta
						local Nbeta_extreme_out = `Nbeta_extreme_out' + (`absCoefOut' >= abs(`coef_`time'`r'_out')) 
					
						// for difference between in vs out p-value we need separate regressions, using "modified" outcome
						g `measure'`time'_m = `measure'`time'
						replace `measure'`time'_m = `measure'`time'_m + `coef_`time'`r'_av' if neighborRI`i' == 1 & neighborRI0 == 0
						replace `measure'`time'_m = `measure'`time'_m - `coef_`time'`r'_av' if neighborRI`i' == 0 & neighborRI0 == 1	
						
						// run regression with modified outcome
						areg `measure'`time'_m neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero' `wgt', absorb(sessionXpairStrataXpartyPair)
						local absCoefDiff = abs(_b[neighborRI`i'Xsame`hetero']-_b[neighborRI`i'Xdiff`hetero'])
						local Nbeta_extreme_diff = `Nbeta_extreme_diff' + (`absCoefDiff' >= abs(`diff')) 
						drop `measure'`time'_m
					}
					local pval_`time'`r'_RIin = string(`Nbeta_extreme_in'/`runsRI',"%9.2g")
					local pval_`time'`r'_RIout = string(`Nbeta_extreme_out'/`runsRI',"%9.2g")	
					local pval_`time'`r'_RIdiff = string(`Nbeta_extreme_diff'/`runsRI',"%9.2g")	
				}
				else {
					local pval_`time'`r'_RIin = 999
					local pval_`time'`r'_RIout = 999	
					local pval_`time'`r'_RIdiff = 999										
				}								
				
				// get stars for all pvals
				foreach y in av in out diff RI RIin RIout RIdiff {
					local pstar_`time'`r'_`y' = "" // reset p-value
					if `pval_`time'`r'_`y''<0.10 & `pval_`time'`r'_`y''>=0.05 local pstar_`time'`r'_`y' = "*"
					if `pval_`time'`r'_`y''<0.05 & `pval_`time'`r'_`y''>=0.01 local pstar_`time'`r'_`y' = "**"
					if `pval_`time'`r'_`y''<0.01 local pstar_`time'`r'_`y' = "***"
				}
				
				// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
				foreach y in av in out diff {
					if `pval_`time'`r'_`y''<0.001 local pval_`time'`r'_`y' = "<0.001" 
				}	
		}
	}

		
	// Output Main Effects Table (appendix?)
	estout M1 M2 M3 M4 using "`tablefolder'//pairVoteContestA`wgtlab'.tex", style(tex) replace ///
	keep(xresid) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(xresid "Neighbor")

	estout MLead1 MLead2 MLead3 MLead4 using "`tablefolder'//pairVoteContestB`wgtlab'.tex", style(tex) replace ///
	keep(xresid) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(xresid "Neighbor")

	estout MLag1 MLag2 MLag3 MLag4 using "`tablefolder'//pairVoteContestC`wgtlab'.tex", style(tex) replace ///
	keep(xresid) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(xresid "Neighbor")

	local tex "& \multicolumn{4}{c}{\textit{Panel A:} Contemporaneous Effect (t)} \vspace{0.2em} \\"
	local tex "`tex' \input{pairVoteContestA`wgtlab'} \vspace{-1.2em} \\"
	local tex "`tex' & (`se_1_av') & (`se_2_av') & (`se_3_av') & (`se_4_av') \\"
	local tex "`tex' & [`pval_1_av']`pstar_1_av' & [`pval_2_av']`pstar_2_av' & [`pval_3_av']`pstar_3_av' & [`pval_4_av']`pstar_4_av' \\"
	local tex "`tex' & \{`pval_1_RI'\}`pstar_1_RI' & \{`pval_2_RI'\}`pstar_2_RI' & \{`pval_3_RI'\}`pstar_3_RI' & \{`pval_4_RI'\}`pstar_4_RI' \\ \\[-5pt]"	
	local tex "`tex' Observations & `obs1' & `obs2' & `obs3' & `obs4' \\ \hline"
	local tex "`tex' \vspace{-8pt} & & & & \\"
	local tex "`tex' & \multicolumn{4}{c}{\textit{Panel B:} One Year Later (t+1)} \vspace{0.2em} \\"  
	local tex "`tex' \input{pairVoteContestB`wgtlab'} \vspace{-1.2em} \\" 
	local tex "`tex'  & (`se_Lead1_av') & (`se_Lead2_av') & (`se_Lead3_av') & (`se_Lead4_av') \\"
	local tex "`tex'  & [`pval_Lead1_av']`pstar_Lead1_av' & [`pval_Lead2_av']`pstar_Lead2_av' & [`pval_Lead3_av']`pstar_Lead3_av' & [`pval_Lead4_av']`pstar_Lead4_av' \\"
	local tex "`tex' & \{`pval_Lead1_RI'\}`pstar_Lead1_RI' & \{`pval_Lead2_RI'\}`pstar_Lead2_RI' & \{`pval_Lead3_RI'\}`pstar_Lead3_RI' & \{`pval_Lead4_RI'\}`pstar_Lead4_RI' \\ \\[-5pt]"		
	local tex "`tex' Observations & `obsLead1' & `obsLead2' & `obsLead3' & `obsLead4' \\ \hline" 
	local tex "`tex' \vspace{-8pt} & & & & \\"
	local tex "`tex' & \multicolumn{4}{c}{\textit{Panel C:} Previous Year (Placebo) (t-1)} \vspace{0.2em} \\"  
	local tex "`tex' \input{pairVoteContestC`wgtlab'} \vspace{-1.2em} \\"
	local tex "`tex'  & (`se_Lag1_av') & (`se_Lag2_av') & (`se_Lag3_av') & (`se_Lag4_av') \\"
	local tex "`tex'  & [`pval_Lag1_av']`pstar_Lag1_av' & [`pval_Lag2_av']`pstar_Lag2_av' & [`pval_Lag3_av']`pstar_Lag3_av' & [`pval_Lag4_av']`pstar_Lag4_av' \\"
	local tex "`tex' & \{`pval_Lag1_RI'\}`pstar_Lag1_RI' & \{`pval_Lag2_RI'\}`pstar_Lag2_RI' & \{`pval_Lag3_RI'\}`pstar_Lag3_RI' & \{`pval_Lag4_RI'\}`pstar_Lag4_RI' \\ \\[-5pt]"			
	local tex "`tex' Observations & `obsLag1' & `obsLag2' & `obsLag3' & `obsLag4' \\ \hline" 	
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `mean3' & `mean4' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sd3' & `sd4' \\ \toprule" 
	local tex "`tex' \multicolumn{5}{p{15.7cm}}{\footnotesize \textit{Notes:} `eachPanelFour'" 
	local tex "`tex' `wgttabnote'`contested' `compliance' `similarity'"
	local tex "`tex' `dyadSEp' `riCurly'"	
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `neighbor' `strataDef'" 
	local tex "`tex' `outcomeMeanA' `plab'}" 
	local tex "`tex' \\ \end{tabular} }"
		
	esttab M1 M2 M3 M4 using "`tablefolder'//pairVoteContest`wgtlab'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("Below 50th Votes" "Below 25th Votes", pattern(1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Compliance\\(1)}" "\specialcell{Similarity\\(2)}" ///
	"\specialcell{Compliance\\(3)}" "\specialcell{Similarity\\(4)}")	
	
	// Get sharpened q-values (Anderson 2008)	
	g float pval=.
	local i=1
	foreach x in pval_1_out_raw pval_2_out_raw pval_3_out_raw pval_4_out_raw ///
		pval_1_in_raw pval_2_in_raw pval_3_in_raw pval_4_in_raw ///
		pval_Lead1_out_raw pval_Lead2_out_raw pval_Lead3_out_raw pval_Lead4_out_raw ///
		pval_Lead1_in_raw pval_Lead2_in_raw pval_Lead3_in_raw pval_Lead4_in_raw {
		
		replace pval=``x'' in `i'
		local i=`i'+1
	}
	keep pval
	drop if mi(pval)
		
	run "$path_pch_git/Analysis/fdr_sharpened_qvalues.do"
	
	local i=1
	foreach x in qval_1_out qval_2_out qval_3_out qval_4_out ///
		qval_1_in qval_2_in qval_3_in qval_4_in ///
		qval_Lead1_out qval_Lead2_out qval_Lead3_out qval_Lead4_out ///
		qval_Lead1_in qval_Lead2_in qval_Lead3_in qval_Lead4_in {
		
		local `x' = string(qval,"%9.2g") in `i'
		if qval<0.001 in `i' local `x' = "<0.001" 
		
		local i=`i'+1
	}	
	
	// get stars for qvals
	local i=1
	foreach y in qstar_1_out qstar_2_out qstar_3_out qstar_4_out ///
		qstar_1_in qstar_2_in qstar_3_in qstar_4_in ///
		qstar_Lead1_out qstar_Lead2_out qstar_Lead3_out qstar_Lead4_out ///
		qstar_Lead1_in qstar_Lead2_in qstar_Lead3_in qstar_Lead4_in {
			
		local `y' = "" // reset q-star
		local j = qval in `i' // raw p-value
		
		if `j'<0.10 & `j'>=0.05 local `y' = "*"
		if `j'<0.05 & `j'>=0.01 local `y' = "**"
		if `j'<0.01 local `y' = "***"
				
		local i=`i'+1
	}	

	// Output Heterogeneous Effects Table
	local tex "& \multicolumn{4}{c}{\textit{Panel A:} Contemporaneous Effect (t)} \vspace{0.2em} \\" 
	
	local tex "`tex' Neighbor $\times$ Different `Hetero' & `coef_1_out' & `coef_2_out' & `coef_3_out' & `coef_4_out' \\"
	local tex "`tex' `bipart' & [`pval_1_out']`pstar_1_out' & [`pval_2_out']`pstar_2_out' & [`pval_3_out']`pstar_3_out' & [`pval_4_out']`pstar_4_out' \\"
	local tex "`tex' & \{`pval_1_RIout'\}`pstar_1_RIout' & \{`pval_2_RIout'\}`pstar_2_RIout' & \{`pval_3_RIout'\}`pstar_3_RIout' & \{`pval_4_RIout'\}`pstar_4_RIout' \\"	
	local tex "`tex' & <`qval_1_out'>`qstar_1_out' & <`qval_2_out'>`qstar_2_out' & <`qval_3_out'>`qstar_3_out' & <`qval_4_out'>`qstar_4_out' \\ \\[-5pt]"
	
	local tex "`tex' Neighbor $\times$ Same `Hetero' & `coef_1_in' & `coef_2_in' & `coef_3_in' & `coef_4_in' \\"
	local tex "`tex'  & [`pval_1_in']`pstar_1_in' & [`pval_2_in']`pstar_2_in' & [`pval_3_in']`pstar_3_in' & [`pval_4_in']`pstar_4_in' \\"
	local tex "`tex' & \{`pval_1_RIin'\}`pstar_1_RIin' & \{`pval_2_RIin'\}`pstar_2_RIin' & \{`pval_3_RIin'\}`pstar_3_RIin' & \{`pval_4_RIin'\}`pstar_4_RIin' \\"
	local tex "`tex' & <`qval_1_in'>`qstar_1_in' & <`qval_2_in'>`qstar_2_in' & <`qval_3_in'>`qstar_3_in' & <`qval_4_in'>`qstar_4_in' \\ \\[-5pt]"
	
	*local tex "`tex' Same = Different & [`pval_1_diff']`pstar_1_diff' & [`pval_2_diff']`pstar_2_diff' & [`pval_3_diff']`pstar_3_diff' & [`pval_4_diff']`pstar_4_diff' \\"
	*local tex "`tex' & \{`pval_1_RIdiff'\}`pstar_1_RIdiff' & \{`pval_2_RIdiff'\}`pstar_2_RIdiff' & \{`pval_3_RIdiff'\}`pstar_3_RIdiff' & \{`pval_4_RIdiff'\}`pstar_4_RIdiff' \\"				
	local tex "`tex' Observations & `obsHet1' & `obsHet2' & `obsHet3' & `obsHet4' \\ \hline" 
	local tex "`tex' \vspace{-8pt} & & & & \\"
	
	local tex "`tex' & \multicolumn{4}{c}{\textit{Panel B:} One Year Later (t+1)} \vspace{0.2em} \\"  
	
	local tex "`tex' Neighbor $\times$ Different `Hetero' & `coef_Lead1_out' & `coef_Lead2_out' & `coef_Lead3_out' & `coef_Lead4_out' \\"
	local tex "`tex' `bipart' & [`pval_Lead1_out']`pstar_Lead1_out' & [`pval_Lead2_out']`pstar_Lead2_out' & [`pval_Lead3_out']`pstar_Lead3_out' & [`pval_Lead4_out']`pstar_Lead4_out' \\"
	local tex "`tex' & \{`pval_Lead1_RIout'\}`pstar_Lead1_RIout' & \{`pval_Lead2_RIout'\}`pstar_Lead2_RIout' & \{`pval_Lead3_RIout'\}`pstar_Lead3_RIout' & \{`pval_Lead4_RIout'\}`pstar_Lead4_RIout' \\"	
	local tex "`tex' & <`qval_Lead1_out'>`qstar_Lead1_out' & <`qval_Lead2_out'>`qstar_Lead2_out' & <`qval_Lead3_out'>`qstar_Lead3_out' & <`qval_Lead4_out'>`qstar_Lead4_out' \\ \\[-5pt]"
	
	
	local tex "`tex' Neighbor $\times$ Same `Hetero' & `coef_Lead1_in' & `coef_Lead2_in' & `coef_Lead3_in' & `coef_Lead4_in' \\"
	local tex "`tex'  & [`pval_Lead1_in']`pstar_Lead1_in' & [`pval_Lead2_in']`pstar_Lead2_in' & [`pval_Lead3_in']`pstar_Lead3_in' & [`pval_Lead4_in']`pstar_Lead4_in' \\"
	local tex "`tex' & \{`pval_Lead1_RIin'\}`pstar_Lead1_RIin' & \{`pval_Lead2_RIin'\}`pstar_Lead2_RIin' & \{`pval_Lead3_RIin'\}`pstar_Lead3_RIin' & \{`pval_Lead4_RIin'\}`pstar_Lead4_RIin' \\"
	local tex "`tex' & <`qval_Lead1_in'>`qstar_Lead1_in' & <`qval_Lead2_in'>`qstar_Lead2_in' & <`qval_Lead3_in'>`qstar_Lead3_in' & <`qval_Lead4_in'>`qstar_Lead4_in' \\ \\[-5pt]"
	
	*local tex "`tex' Same = Different & [`pval_Lead1_diff']`pstar_Lead1_diff' & [`pval_Lead2_diff']`pstar_Lead2_diff' & [`pval_Lead3_diff']`pstar_Lead3_diff' & [`pval_Lead4_diff']`pstar_Lead4_diff' \\"
	*local tex "`tex' & \{`pval_Lead1_RIdiff'\}`pstar_Lead1_RIdiff' & \{`pval_Lead2_RIdiff'\}`pstar_Lead2_RIdiff' & \{`pval_Lead3_RIdiff'\}`pstar_Lead3_RIdiff' & \{`pval_Lead4_RIdiff'\}`pstar_Lead4_RIdiff' \\"				
	local tex "`tex' Observations & `obsHetLead1' & `obsHetLead2' & `obsHetLead3' & `obsHetLead4' \\ \hline" 
	local tex "`tex' \vspace{-8pt} & & & & \\"
	
	local tex "`tex' & \multicolumn{4}{c}{\textit{Panel C:} Previous Year (Placebo) (t-1)} \vspace{0.2em} \\"  

	local tex "`tex' Neighbor $\times$ Different `Hetero' & `coef_Lag1_out' & `coef_Lag2_out' & `coef_Lag3_out' & `coef_Lag4_out' \\"
	local tex "`tex' `bipart' & [`pval_Lag1_out']`pstar_Lag1_out' & [`pval_Lag2_out']`pstar_Lag2_out' & [`pval_Lag3_out']`pstar_Lag3_out' & [`pval_Lag4_out']`pstar_Lag4_out' \\"
	local tex "`tex' & \{`pval_Lag1_RIout'\}`pstar_Lag1_RIout' & \{`pval_Lag2_RIout'\}`pstar_Lag2_RIout' & \{`pval_Lag3_RIout'\}`pstar_Lag3_RIout' & \{`pval_Lag4_RIout'\}`pstar_Lag4_RIout' \\ \\[-5pt]"				
	
	local tex "`tex' Neighbor $\times$ Same `Hetero' & `coef_Lag1_in' & `coef_Lag2_in' & `coef_Lag3_in' & `coef_Lag4_in' \\"
	local tex "`tex'  & [`pval_Lag1_in']`pstar_Lag1_in' & [`pval_Lag2_in']`pstar_Lag2_in' & [`pval_Lag3_in']`pstar_Lag3_in' & [`pval_Lag4_in']`pstar_Lag4_in' \\"
	local tex "`tex' & \{`pval_Lag1_RIin'\}`pstar_Lag1_RIin' & \{`pval_Lag2_RIin'\}`pstar_Lag2_RIin' & \{`pval_Lag3_RIin'\}`pstar_Lag3_RIin' & \{`pval_Lag4_RIin'\}`pstar_Lag4_RIin' \\ \\[-5pt]"			
	
	*local tex "`tex' Same = Different & [`pval_Lag1_diff']`pstar_Lag1_diff' & [`pval_Lag2_diff']`pstar_Lag2_diff' & [`pval_Lag3_diff']`pstar_Lag3_diff' & [`pval_Lag4_diff']`pstar_Lag4_diff' \\"
	*local tex "`tex' & \{`pval_Lag1_RIdiff'\}`pstar_Lag1_RIdiff' & \{`pval_Lag2_RIdiff'\}`pstar_Lag2_RIdiff' & \{`pval_Lag3_RIdiff'\}`pstar_Lag3_RIdiff' & \{`pval_Lag4_RIdiff'\}`pstar_Lag4_RIdiff' \\"				
	local tex "`tex' Observations & `obsHetLag1' & `obsHetLag2' & `obsHetLag3' & `obsHetLag4' \\ \hline" 	
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y \\"
	
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `mean3' & `mean4' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sd3' & `sd4' \\ \toprule" 
	
	local tex "`tex' \multicolumn{5}{p{13.5cm}}{\footnotesize \textit{Notes:} `eachPanelFour'" 
	local tex "`tex' `wgttabnote'`contested' `compliance' `similarity'"
	local tex "`tex' `dyadSquare' `riCurly' `qlab'"	
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `samehet'"
	local tex "`tex' `outcomeMeanA' `plab'}" 
	local tex "`tex' \\ \end{tabular} }"
	
	esttab Het1 Het2 Het3 Het4 using "`tablefolder'//pairVoteContest`Hetero'`wgtlab'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("Below 50th Votes" "Below 25th Votes", pattern(1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Compliance\\(1)}" "\specialcell{Similarity\\(2)}" ///
	"\specialcell{Compliance\\(3)}" "\specialcell{Similarity\\(4)}")	
	
	timer off 2
	disp "DONE dyadVoteContest"	
	timer list 2

	beep
}
if `dyadVoteCornerDiff'==1 { // Dyadic voting results (corner vs. middle)

	timer on 3
	forv k = 1/3 { 		// Time period?
		local time: word `k' of "Lag" "" "Lead"

		forv r = 1/2 {	// Type of voting/cosponsorship measure?
			local measure: word `r' of "samevote4c" "voteSim4c"
				
				dis "`measure'`time'"
				use `datamod', clear
				keep session_id samevote?c* votediff?c* voteSim?c* *RI* minMP maxMP pairStrata samecoalition sameparty ///
				samegender specialSession shortSession firstSession lastSession leadShortIncomplete	party_id party_id_d ///
				sessionXpartyPair
								
				keep if pairStrata==2 // no-one in dyad pre-assigned seat
				drop if sameparty==1 // keep only diff-party dyads
				
				if `k'==1 {
					drop if firstSession==1 
				}
				else if `k'==3 {
					drop if lastSession==1
					drop if leadShortIncomplete==1
				}	
				
				/* Hetero: corner vs. middle effect */
				
				// avg. effect for use in RI
				qui areg `measure'`time' neighborRI0, absorb(sessionXpartyPair)
				local coef_`time'`r'_av = _b[neighborRI0]
				
				// residualize
				qui areg `measure'`time' session_id#cornerRI0 if !mi(neighborRI0XcornerRI0) & !mi(neighborRI0XmiddleRI0) & !mi(`measure'`time'), absorb(sessionXpartyPair)
				local numN = e(N)
				local numDummy = e(df_m)+e(df_a) 
				predict yresid, residuals
				qui areg neighborRI0XcornerRI0 session_id#cornerRI0 if !mi(neighborRI0XcornerRI0) & !mi(neighborRI0XmiddleRI0) & !mi(`measure'`time'), absorb(sessionXpartyPair)
				local numN2 = e(N)
				predict xoutresid, residuals
				assert `numN' == `numN2'
				qui areg neighborRI0XmiddleRI0 session_id#cornerRI0 if !mi(neighborRI0XcornerRI0) & !mi(neighborRI0XmiddleRI0) & !mi(`measure'`time'), absorb(sessionXpartyPair)
				predict xinresid, residuals
				
				// run regression
				xi: regdyad2 yresid xinresid xoutresid if !mi(neighborRI0XcornerRI0) & !mi(neighborRI0XmiddleRI0) & !mi(`measure'`time') ///
					& !mi(party_id) & !mi(party_id_d), dyads(minMP maxMP)
				est sto Het`time'`r'
				local obsHet`time'`r' = e(N)
				local numReg = 2
				matrix varMatrix = e(V)
				
				// summary stats
				qui su `measure'`time' if e(sample)==1
				local mean`time'`r' = string(r(mean),"%9.2g")
				local sd`time'`r' = string(r(sd),"%9.2g")
				local sd`time'`r'raw = `r(sd)'
				
				// prepare coefficient, standard error, p-value for export					
				local coef_`time'`r'_in = string(_b[xinresid],"%9.2g")
				local coef_`time'`r'_out = string(_b[xoutresid],"%9.2g")
				local se_`time'`r'_in = _se[xinresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
				local se_`time'`r'_out = _se[xoutresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
				local t_df = `numN'-`numReg'-`numDummy'-1
				local pval_`time'`r'_in =  string(ttail(`t_df',abs(`coef_`time'`r'_in'/`se_`time'`r'_in'))*2,"%9.2g")
				local pval_`time'`r'_out =  string(ttail(`t_df',abs(`coef_`time'`r'_out'/`se_`time'`r'_out'))*2,"%9.2g")
				
				// pval calculation for the difference
				local diff = `coef_`time'`r'_in' - `coef_`time'`r'_out'
				local se_`time'`r'_diff = sqrt(varMatrix[1,1]+varMatrix[2,2]-2*varMatrix[2,1])
				local pval_`time'`r'_diff =  string(ttail(`t_df',abs(`diff'/`se_`time'`r'_diff'))*2,"%9.2g")
				drop yresid xinresid xoutresid
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme_in = 0
					local Nbeta_extreme_out = 0
					local Nbeta_extreme_diff = 0
					forv i=1/`runsRI' {
						areg `measure'`time' neighborRI`i'XmiddleRI`i' neighborRI`i'XcornerRI`i' session_id#cornerRI`i', absorb(sessionXpartyPair)
						local absCoefIn = abs(_b[neighborRI`i'XmiddleRI`i'])
						local absCoefOut = abs(_b[neighborRI`i'XcornerRI`i'])
						local Nbeta_extreme_in = `Nbeta_extreme_in' + (`absCoefIn' >= abs(`coef_`time'`r'_in')) // + 1 every time see more extreme beta
						local Nbeta_extreme_out = `Nbeta_extreme_out' + (`absCoefOut' >= abs(`coef_`time'`r'_out')) 
					
						// for difference between middle vs corner p-value we need separate regressions, using "modified" outcome
						g `measure'`time'_m = `measure'`time'
						replace `measure'`time'_m = `measure'`time'_m + `coef_`time'`r'_av' if neighborRI`i' == 1 & neighborRI0 == 0
						replace `measure'`time'_m = `measure'`time'_m - `coef_`time'`r'_av' if neighborRI`i' == 0 & neighborRI0 == 1	
						
						// run regression with modified outcome
						areg `measure'`time'_m neighborRI`i'XmiddleRI`i' neighborRI`i'XcornerRI`i' session_id#cornerRI`i', absorb(sessionXpartyPair)
						local absCoefDiff = abs(_b[neighborRI`i'XmiddleRI`i']-_b[neighborRI`i'XcornerRI`i'])
						local Nbeta_extreme_diff = `Nbeta_extreme_diff' + (`absCoefDiff' >= abs(`diff')) 
						drop `measure'`time'_m
					}
					local pval_`time'`r'_RIin = string(`Nbeta_extreme_in'/`runsRI',"%9.2g")
					local pval_`time'`r'_RIout = string(`Nbeta_extreme_out'/`runsRI',"%9.2g")	
					local pval_`time'`r'_RIdiff = string(`Nbeta_extreme_diff'/`runsRI',"%9.2g")	
				}
				else {
					local pval_`time'`r'_RIin = 999
					local pval_`time'`r'_RIout = 999	
					local pval_`time'`r'_RIdiff = 999					
				}
				
				// get stars for all pvals
				foreach y in in out diff RIin RIout RIdiff {
					local pstar_`time'`r'_`y' = "" // reset p-value
					if `pval_`time'`r'_`y''<0.10 & `pval_`time'`r'_`y''>=0.05 local pstar_`time'`r'_`y' = "*"
					if `pval_`time'`r'_`y''<0.05 & `pval_`time'`r'_`y''>=0.01 local pstar_`time'`r'_`y' = "**"
					if `pval_`time'`r'_`y''<0.01 local pstar_`time'`r'_`y' = "***"
				}
				
				// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
				foreach y in in out diff {
					if `pval_`time'`r'_`y''<0.001 local pval_`time'`r'_`y' = "<0.001" 
				}					
		}
	}
	
	// Output Table
	local tex "Neighbor $\times$ Corner & `coef_1_out' & `coef_2_out' & `coef_Lead1_out' & `coef_Lead2_out' & `coef_Lag1_out' & `coef_Lag2_out' \\"
	local tex "`tex' & [`pval_1_out']`pstar_1_out' & [`pval_2_out']`pstar_2_out' & [`pval_Lead1_out']`pstar_Lead1_out' & [`pval_Lead2_out']`pstar_Lead2_out' & [`pval_Lag1_out']`pstar_Lag1_out' & [`pval_Lag2_out']`pstar_Lag2_out' \\"
	local tex "`tex' & \{`pval_1_RIout'\}`pstar_1_RIout' & \{`pval_2_RIout'\}`pstar_2_RIout' & \{`pval_Lead1_RIout'\}`pstar_Lead1_RIout' & \{`pval_Lead2_RIout'\}`pstar_Lead2_RIout' & \{`pval_Lag1_RIout'\}`pstar_Lag1_RIout' & \{`pval_Lag2_RIout'\}`pstar_Lag2_RIout' \\ \\[-5pt]"		
	
	local tex "`tex' Neighbor $\times$ Middle & `coef_1_in' & `coef_2_in' & `coef_Lead1_in' & `coef_Lead2_in'  & `coef_Lag1_in' & `coef_Lag2_in' \\"
	local tex "`tex'  & [`pval_1_in']`pstar_1_in' & [`pval_2_in']`pstar_2_in' & [`pval_Lead1_in']`pstar_Lead1_in' & [`pval_Lead2_in']`pstar_Lead2_in' & [`pval_Lag1_in']`pstar_Lag1_in' & [`pval_Lag2_in']`pstar_Lag2_in' \\"
	local tex "`tex' & \{`pval_1_RIin'\}`pstar_1_RIin' & \{`pval_2_RIin'\}`pstar_2_RIin' & \{`pval_Lead1_RIin'\}`pstar_Lead1_RIin' & \{`pval_Lead2_RIin'\}`pstar_Lead2_RIin' & \{`pval_Lag1_RIin'\}`pstar_Lag1_RIin' & \{`pval_Lag2_RIin'\}`pstar_Lag2_RIin' \\ \\[-5pt]"		
		
	local tex "`tex' Corner = Middle & [`pval_1_diff']`pstar_1_diff' & [`pval_2_diff']`pstar_2_diff' & [`pval_Lead1_diff']`pstar_Lead1_diff' & [`pval_Lead2_diff']`pstar_Lead2_diff' & [`pval_Lag1_diff']`pstar_Lag1_diff' & [`pval_Lag2_diff']`pstar_Lag2_diff' \\"
	local tex "`tex' & \{`pval_1_RIdiff'\}`pstar_1_RIdiff' & \{`pval_2_RIdiff'\}`pstar_2_RIdiff' & \{`pval_Lead1_RIdiff'\}`pstar_Lead1_RIdiff' & \{`pval_Lead2_RIdiff'\}`pstar_Lead2_RIdiff' & \{`pval_Lag1_RIdiff'\}`pstar_Lag1_RIdiff' & \{`pval_Lag2_RIdiff'\}`pstar_Lag2_RIdiff' \\ \\[-5pt] \hline"				
	
	local tex "`tex' Observations & `obsHet1' & `obsHet2' & `obsHetLead1' & `obsHetLead2' & `obsHetLag1' & `obsHetLag2' \\" 
	local tex "`tex' Session $\times$ Corner FE & Y & Y & Y & Y & Y & Y \\"	
	local tex "`tex' Session $\times$ Party Pair FE & Y & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `meanLead1' & `meanLead2' & `meanLag1' & `meanLag2' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sdLead1' & `sdLead2' & `sdLag1' & `sdLag2' \\ \toprule" 
	
	local tex "`tex' \multicolumn{7}{p{18cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `diffPartyOnly'"
	local tex "`tex' `compliance' `similarity'"
	local tex "`tex' `neighbor' `corner'"
	local tex "`tex' `dyadSquare' `riCurly'"
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `plab'}"
	local tex "`tex' \\ \end{tabular} }"
	
	esttab Het1 Het2 HetLead1 HetLead2 HetLag1 HetLag2 using "`tablefolder'//pairVoteCornerDiff.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("\specialcell{Contemporaneous\\Effect (t)}" "\specialcell{One Year\\Later (t+1)}" ///
			"\specialcell{Previous Year\\(Placebo) (t-1)}", pattern(1 0 1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Compliance\\(1)}" "\specialcell{Similarity\\(2)}" ///
	"\specialcell{Compliance\\(3)}" "\specialcell{Similarity\\(4)}" ///	
	"\specialcell{Compliance\\(5)}" "\specialcell{Similarity\\(6)}")	
	
	timer off 3
	disp "DONE dyadVoteCornerDiff"
	timer list 3

}
if `mpVote'==1 { // MP-session-level voting results

	timer on 4
	use "`votingfolder'/MP_session_analysis", clear
	
	/* Calculate weights */
	
	if `reweight'==1 {
		bys strataSessionPartyFE: egen probHalf = mean(halfOther`Hetero'NeighborRI0)
		bys strataSessionPartyFE: egen probAll = mean(allOther`Hetero'NeighborRI0)
		g noOther`Hetero'NeighborRI0 = halfOther`Hetero'NeighborRI0==0 & allOther`Hetero'NeighborRI0==0 & !mi(halfOther`Hetero'NeighborRI0)
		bys strataSessionPartyFE: egen probNone = mean(noOther`Hetero'NeighborRI0)
		
		g invProb = 1/probHalf if halfOther`Hetero'NeighborRI0==1
		replace invProb = 1/probAll if allOther`Hetero'NeighborRI0==1
		replace invProb = 1/probNone if noOther`Hetero'NeighborRI0==1
	}

	// Define sessions that we will use
	g sampleSession = (!specialSession) & (!shortSession) & !(session_id == 149)
	g sampleSessionLag = !inlist(session_id,115,119,120,124,125,129,130,134,135,137,138,142,143,146,147,148,149)
	g sampleSessionLead = !inlist(session_id,118,119,123,124,128,129,133,134,136,137,141,142,145,146,147,148,149)

	// Three panels: contemporary, lead, lag (placebo)
	forv k = 1/3 { 		// Time period?
		local time: word `k' of "Lag" "" "Lead"	
	
		forv r = 1/2 {	// Type of voting/cosponsorship measure?
			local measure: word `r' of "non_compliance" "rebelWeak"
			
			areg `measure'`time' fracOther`Hetero'NeighborRI0 if sampleSession`time' `wgt', absorb(strataSessionPartyFE) cluster(MP_id)
			est sto MP`time'`r'
			local obs`time'`r' = e(N)	
			
			// summary stats
			qui su `measure'`time' if e(sample)==1
			local mean`time'`r' = string(r(mean),"%9.2g")
			local sd`time'`r' = string(r(sd),"%9.2g")
			local sd`time'`r'raw = `r(sd)'	
			
			// prepare coefficient, standard error, p-value for export
			local coef_`time'`r'_av = _b[fracOther`Hetero'NeighborRI0]
			local coef_`time'`r'_avs = string(_b[fracOther`Hetero'NeighborRI0],"%9.2g")			
			local se_`time'`r'_av = string(_se[fracOther`Hetero'NeighborRI0],"%9.2g")
			local pval_`time'`r'_av = string(ttail(e(df_r),abs(`coef_`time'`r'_av'/`se_`time'`r'_av'))*2,"%9.2g")	
			local pval_`time'`r'_av_raw = ttail(e(df_r),abs(`coef_`time'`r'_av'/`se_`time'`r'_av'))*2
							
			// diff-party point estimate and 95% confidence interval for figure
			if `k'==2 {
				local mp_`r'_out = `coef_`r'_av'/`sd`r'raw'
				local mpl_`r'_out = (`coef_`r'_av' - invttail(e(df_r),0.025)*`se_`r'_av')/`sd`r'raw'
				local mpu_`r'_out = (`coef_`r'_av' + invttail(e(df_r),0.025)*`se_`r'_av')/`sd`r'raw'
			}		
			
			// randomization inference p-values
			if `RI'==1 {
				local Nbeta_extreme = 0
				forv i=1/`runsRI' {
					
					if `reweight'==1 {
						drop probHalf probAll probNone invProb 
						
						bys strataSessionPartyFE: egen probHalf = mean(halfOther`Hetero'NeighborRI`i')
						bys strataSessionPartyFE: egen probAll = mean(allOther`Hetero'NeighborRI`i')
						g noOther`Hetero'NeighborRI`i' = halfOther`Hetero'NeighborRI`i'==0 & ///
								allOther`Hetero'NeighborRI`i'==0 & !mi(halfOther`Hetero'NeighborRI`i')
						bys strataSessionPartyFE: egen probNone = mean(noOther`Hetero'NeighborRI`i')
						
						g invProb = 1/probHalf if halfOther`Hetero'NeighborRI`i'==1
						replace invProb = 1/probAll if allOther`Hetero'NeighborRI`i'==1
						replace invProb = 1/probNone if noOther`Hetero'NeighborRI`i'==1
						
						drop noOther`Hetero'NeighborRI`i'
					}
					
					areg `measure'`time' fracOther`Hetero'NeighborRI`i' if sampleSession`time' `wgt', absorb(strataSessionPartyFE)
					local absCoef = abs(_b[fracOther`Hetero'NeighborRI`i'])
					local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`time'`r'_av')) // + 1 every time see more extreme beta
				}
				local pval_`time'`r'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
			}
			else {
				local pval_`time'`r'_RI = 999
			}
			
			// get stars for all pvals
			foreach y in av RI {
				local pstar_`time'`r'_`y' = "" // reset p-value
				if `pval_`time'`r'_`y''<0.10 & `pval_`time'`r'_`y''>=0.05 local pstar_`time'`r'_`y' = "*"
				if `pval_`time'`r'_`y''<0.05 & `pval_`time'`r'_`y''>=0.01 local pstar_`time'`r'_`y' = "**"
				if `pval_`time'`r'_`y''<0.01 local pstar_`time'`r'_`y' = "***"
			}	
			
			// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
			if `pval_`time'`r'_av'<0.001 local pval_`time'`r'_av = "<0.001" 			
		}
	}
	
	// Get sharpened q-values (Anderson 2008)	
	g float pval=.
	local i=1
	foreach x in pval_1_av_raw pval_2_av_raw pval_Lead1_av_raw pval_Lead1_av_raw {
		replace pval=``x'' in `i'
		local i=`i'+1
	}
	keep pval
	drop if mi(pval)
		
	run "$path_pch_git/Analysis/fdr_sharpened_qvalues.do"
	
	local i=1
	foreach x in qval_1_av qval_2_av qval_Lead1_av qval_Lead2_av {
		
		local `x' = string(qval,"%9.2g") in `i'
		if qval<0.001 in `i' local `x' = "<0.001" 
		
		local i=`i'+1
	}	
	
	// get stars for qvals
	local i=1
	foreach y in qstar_1_av qstar_2_av qstar_Lead1_av qstar_Lead2_av {
			
		local `y' = "" // reset q-star
		local j = qval in `i' // raw p-value
		
		if `j'<0.10 & `j'>=0.05 local `y' = "*"
		if `j'<0.05 & `j'>=0.01 local `y' = "**"
		if `j'<0.01 local `y' = "***"
				
		local i=`i'+1
	}		
	
	// Output Main Effects Table
	estout MP1 MP2 MPLead1 MPLead2 MPLag1 MPLag2 using "`tablefolder'//mpVote`Hetero'A`wgtlab'.tex", style(tex) replace ///
	keep(fracOther`Hetero'NeighborRI0) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(fracOther`Hetero'NeighborRI0 "Proportion Other-`Hetero' Neighbor")

	*local tex "\input{mpVote`Hetero'A`wgtlab'} \vspace{-1.2em} \\"
	local tex "Proportion Other-`Hetero' & `coef_1_avs' & `coef_2_avs' & `coef_Lead1_avs' & `coef_Lead2_avs' & `coef_Lag1_avs' & `coef_Lag2_avs' \\"
	local tex "`tex' Neighbor & (`se_1_av') & (`se_2_av') & (`se_Lead1_av') & (`se_Lead2_av') & (`se_Lag1_av') & (`se_Lag2_av') \\"
	local tex "`tex' & [`pval_1_av']`pstar_1_av' & [`pval_2_av']`pstar_2_av' & [`pval_Lead1_av']`pstar_Lead1_av' & [`pval_Lead2_av']`pstar_Lead2_av' & [`pval_Lag1_av']`pstar_Lag1_av' & [`pval_Lag2_av']`pstar_Lag2_av' \\"
	local tex "`tex' & \{`pval_1_RI'\}`pstar_1_RI' & \{`pval_2_RI'\}`pstar_2_RI' &  \{`pval_Lead1_RI'\}`pstar_Lead1_RI' & \{`pval_Lead2_RI'\}`pstar_Lead2_RI' & \{`pval_Lag1_RI'\}`pstar_Lag1_RI' & \{`pval_Lag2_RI'\}`pstar_Lag2_RI' \\"
	local tex "`tex' & <`qval_1_av'>`qstar_1_av' & <`qval_2_av'>`qstar_2_av' & <`qval_Lead1_av'>`qstar_Lead1_av' & <`qval_Lead2_av'>`qstar_Lead2_av' & & \\ \\[-5pt] \hline"	
	
	local tex "`tex' Observations & `obs1' & `obs2' & `obsLead1' & `obsLead2' & `obsLag1' & `obsLag2' \\"
	local tex "`tex' Session $\times$ Party $\times$ Strata FE & Y & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `meanLead1' & `meanLead2' & `meanLag1' & `meanLag2' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sdLead1' & `sdLead2' & `sdLag1' & `sdLag2' \\ \toprule" 
	
	local tex "`tex' \multicolumn{7}{p{17.8cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `wgttabnote'`noncomplianceMP' `rebelWeakMP' `propothMP'"
	local tex "`tex' `mpSEp' `riCurly' `qlab'"
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDefMP' `plab'}"	
	local tex "`tex' \\ \end{tabular} }"
		
	esttab MP1 MP2 MPLead1 MPLead2 MPLag1 MPLag2 using "`tablefolder'//mpVote`Hetero'`wgtlab'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("\specialcell{Contemporaneous\\Effect (t)}" "\specialcell{One Year\\Later (t+1)}" ///
			"\specialcell{Previous Year\\(Placebo) (t-1)}", pattern(1 0 1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Leader\\Non-\\Compliance\\(1)}" "\specialcell{\\Rebel\\Rate\\(2)}" ///
	"\specialcell{Leader\\Non-\\Compliance\\(3)}" "\specialcell{\\Rebel\\Rate\\(4)}" ///	
	"\specialcell{Leader\\Non-\\Compliance\\(5)}" "\specialcell{\\Rebel\\Rate\\(6)}")		
	
	timer off 4
	disp "DONE mpVote"
	timer list 4
}
if `dyadCospon'==1 { // Dyadic cosponsorship results

	timer on 5
	forv k = 1/3 { 		// Time period?
		local time: word `k' of "Lag" "" "Lead"

		forv r = 1/2 {	// Type of voting/cosponsorship measure?
			local measure: word `r' of "omni_coany" "omni_ihs_coany"
				
				dis "`measure'`time'"
				use `datamod', clear
				keep session_id *coany* *RI* minMP maxMP pairStrata samecoalition sameparty ///
				samegender specialSession shortSession firstSession lastSession leadShortIncomplete	party_id party_id_d	///
				sessionXpairStrataXpartyPair
				
				if `k'==1 {
					drop if firstSession==1 
				}
				else if `k'==3 {
					drop if lastSession==1
					drop if leadShortIncomplete==1
				}
				
				/* Neighborhood effect */
				
				// residualize
				qui areg `measure'`time' if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
				local numN = e(N)
				local numDummy = e(df_a) 
				predict yresid, residuals
				qui areg neighborRI0 if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
				local numN2 = e(N)
				predict xresid, residuals
				assert `numN' == `numN2'
				
				// run regression
				xi: regdyad2 yresid xresid if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d), ///
					dyads(minMP maxMP)
				est sto M`time'`r'
				local obs`time'`r' = e(N)
				qui su `measure'`time' if e(sample)==1
				local mean`time'`r' = string(r(mean),"%9.2g")
				local sd`time'`r' = string(r(sd),"%9.2g")
				local sd`time'`r'raw = `r(sd)'
				local numReg = 1
				
				// prepare coefficient, standard error, p-value for export
				local coef_`time'`r'_av = _b[xresid]
				local se_`time'`r'_av = string(_se[xresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1)),"%9.2g")
				local t_df = `numN'-`numReg'-`numDummy'-1
				local pval_`time'`r'_av = string(ttail(`t_df',abs(`coef_`time'`r'_av'/`se_`time'`r'_av'))*2,"%9.2g")					
				drop yresid xresid	
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme = 0
					forv i=1/`runsRI' {
						areg `measure'`time' neighborRI`i', absorb(sessionXpairStrataXpartyPair)
						local absCoef = abs(_b[neighborRI`i'])
						local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`time'`r'_av')) // + 1 every time see more extreme beta
					}
					local pval_`time'`r'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
				}
				else {
					local pval_`time'`r'_RI = 999
				}				
				
				/* Hetero: party ingroup and outgroup effect */
				
				// residualize			
				qui areg `measure'`time' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure'`time') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair)
				local numN = e(N)
				local numDummy = e(df_a) 
				predict yresid, residuals
				
				qui areg neighborRI0Xsame`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure'`time') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
				local numN2 = e(N)
				predict xinresid, residuals
				assert `numN' == `numN2'
				qui areg neighborRI0Xdiff`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure'`time') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
				predict xoutresid, residuals
				
				// run regression
				xi: regdyad2 yresid xinresid xoutresid if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure'`time') ///
					& !mi(pairStrata) & !mi(party_id) & !mi(party_id_d), dyads(minMP maxMP)
				est sto Het`time'`r'
				local obsHet`time'`r' = e(N)
				local numReg = 2
				matrix varMatrix = e(V)
				
				// prepare coefficient, standard error, p-value for export					
				local coef_`time'`r'_in = string(_b[xinresid],"%9.2g")
				local coef_`time'`r'_out = string(_b[xoutresid],"%9.2g")
				local se_`time'`r'_in = _se[xinresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
				local se_`time'`r'_out = _se[xoutresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
				local t_df = `numN'-`numReg'-`numDummy'-1
				local pval_`time'`r'_in =  string(ttail(`t_df',abs(`coef_`time'`r'_in'/`se_`time'`r'_in'))*2,"%9.2g")
				local pval_`time'`r'_out =  string(ttail(`t_df',abs(`coef_`time'`r'_out'/`se_`time'`r'_out'))*2,"%9.2g")
				
				// pval calculation for the difference
				local diff = `coef_`time'`r'_in' - `coef_`time'`r'_out'
				local se_`time'`r'_diff = sqrt(varMatrix[1,1]+varMatrix[2,2]-2*varMatrix[2,1])
				local pval_`time'`r'_diff =  string(ttail(`t_df',abs(`diff'/`se_`time'`r'_diff'))*2,"%9.2g")
				drop yresid xinresid xoutresid
				
				// diff-party point estimate and 95% confidence interval for figure
				if `k'==2 {
					local core_`r'_out = `coef_`r'_out'/`sd`r'raw'
					local corel_`r'_out = (`coef_`r'_out' - invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
					local coreu_`r'_out = (`coef_`r'_out' + invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
				}
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme_in = 0
					local Nbeta_extreme_out = 0
					local Nbeta_extreme_diff = 0
					forv i=1/`runsRI' {
						areg `measure'`time' neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero', absorb(sessionXpairStrataXpartyPair)
						local absCoefIn = abs(_b[neighborRI`i'Xsame`hetero'])
						local absCoefOut = abs(_b[neighborRI`i'Xdiff`hetero'])
						local Nbeta_extreme_in = `Nbeta_extreme_in' + (`absCoefIn' >= abs(`coef_`time'`r'_in')) // + 1 every time see more extreme beta
						local Nbeta_extreme_out = `Nbeta_extreme_out' + (`absCoefOut' >= abs(`coef_`time'`r'_out')) 
					
						// for difference between in vs out p-value we need separate regressions, using "modified" outcome
						g `measure'`time'_m = `measure'`time'
						replace `measure'`time'_m = `measure'`time'_m + `coef_`time'`r'_av' if neighborRI`i' == 1 & neighborRI0 == 0
						replace `measure'`time'_m = `measure'`time'_m - `coef_`time'`r'_av' if neighborRI`i' == 0 & neighborRI0 == 1	
						
						// run regression with modified outcome
						areg `measure'`time'_m neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero', absorb(sessionXpairStrataXpartyPair)
						local absCoefDiff = abs(_b[neighborRI`i'Xsame`hetero']-_b[neighborRI`i'Xdiff`hetero'])
						local Nbeta_extreme_diff = `Nbeta_extreme_diff' + (`absCoefDiff' >= abs(`diff')) 
						drop `measure'`time'_m
					}
					local pval_`time'`r'_RIin = string(`Nbeta_extreme_in'/`runsRI',"%9.2g")
					local pval_`time'`r'_RIout = string(`Nbeta_extreme_out'/`runsRI',"%9.2g")	
					local pval_`time'`r'_RIdiff = string(`Nbeta_extreme_diff'/`runsRI',"%9.2g")	
				}
				else {
					local pval_`time'`r'_RIin = 999
					local pval_`time'`r'_RIout = 999	
					local pval_`time'`r'_RIdiff = 999										
				}					
				
				// get stars for all pvals
				foreach y in av in out diff RI RIin RIout RIdiff {
					local pstar_`time'`r'_`y' = "" // reset p-value
					if `pval_`time'`r'_`y''<0.10 & `pval_`time'`r'_`y''>=0.05 local pstar_`time'`r'_`y' = "*"
					if `pval_`time'`r'_`y''<0.05 & `pval_`time'`r'_`y''>=0.01 local pstar_`time'`r'_`y' = "**"
					if `pval_`time'`r'_`y''<0.01 local pstar_`time'`r'_`y' = "***"
				}
				
				// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
				foreach y in av in out diff {
					if `pval_`time'`r'_`y''<0.001 local pval_`time'`r'_`y' = "<0.001" 
				}						
		}
	}
	
	// Get sharpened q-values (Anderson 2008)	
	g float pval=.
	local i=1
	foreach x in pval_1_out pval_2_out pval_Lead1_out pval_Lead2_out ///
		pval_1_in pval_2_in pval_Lead1_in pval_Lead2_in {
		
		replace pval=``x'' in `i'
		local i=`i'+1
	}
	keep pval
	drop if mi(pval)
		
	run "$path_pch/Code/analysis/fdr_sharpened_qvalues.do"
	
	local i=1
	foreach x in qval_1_out qval_2_out qval_Lead1_out qval_Lead2_out ///
		qval_1_in qval_2_in qval_Lead1_in qval_Lead2_in {
		
		local `x' = string(qval,"%9.2g") in `i'
		if qval<0.001 in `i' local `x' = "<0.001" 
		
		local i=`i'+1
	}	
	
	// get stars for qvals
	local i=1
	foreach y in qstar_1_out qstar_2_out qstar_Lead1_out qstar_Lead2_out qstar_1_in qstar_2_in qstar_Lead1_in qstar_Lead2_in {
		local `y' = "" // reset q-star
		local j = qval in `i' // raw p-value
		
		if `j'<0.10 & `j'>=0.05 local `y' = "*"
		if `j'<0.05 & `j'>=0.01 local `y' = "**"
		if `j'<0.01 local `y' = "***"
				
		local i=`i'+1
	}	
		
	// Output Main Effects Table (online appendix)
	estout M1 M2 MLead1 MLead2 MLag1 MLag2 using "`tablefolder'//pairCosponA.tex", style(tex) replace ///
	keep(xresid) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(xresid "Neighbor")

	local tex "\input{pairCosponA} \vspace{-1.2em} \\"
	local tex "`tex' & (`se_1_av') & (`se_2_av') & (`se_Lead1_av') & (`se_Lead2_av') & (`se_Lag1_av') & (`se_Lag2_av') \\"
	local tex "`tex' & [`pval_1_av']`pstar_1_av' & [`pval_2_av']`pstar_2_av' & [`pval_Lead1_av']`pstar_Lead1_av' & [`pval_Lead2_av']`pstar_Lead2_av' & [`pval_Lag1_av']`pstar_Lag1_av' & [`pval_Lag2_av']`pstar_Lag2_av' \\"
	local tex "`tex' & \{`pval_1_RI'\}`pstar_1_RI' & \{`pval_2_RI'\}`pstar_2_RI' &  \{`pval_Lead1_RI'\}`pstar_Lead1_RI' & \{`pval_Lead2_RI'\}`pstar_Lead2_RI' & \{`pval_Lag1_RI'\}`pstar_Lag1_RI' & \{`pval_Lag2_RI'\}`pstar_Lag2_RI' \\ \\[-5pt] \hline"	
	
	local tex "`tex' Observations & `obs1' & `obs2' & `obsLead1' & `obsLead2' & `obsLag1' & `obsLag2' \\"
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `meanLead1' & `meanLead2' & `meanLag1' & `meanLag2' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sdLead1' & `sdLead2' & `sdLag1' & `sdLag2' \\ \toprule" 
	
	local tex "`tex' \multicolumn{7}{p{16cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `rawnumb' `ihsAll'"
	local tex "`tex' `neighbor'"
	local tex "`tex' `dyadSEp' `riCurly'" 
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `plab'}"	
	local tex "`tex' \\ \end{tabular} }"
	
	esttab M1 M2 MLead1 MLead2 MLag1 MLag2 using "`tablefolder'//pairCospon.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("\specialcell{Contemporaneous\\Effect (t)}" "\specialcell{One Year\\Later (t+1)}" ///
			"\specialcell{Previous Year\\(Placebo) (t-1)}", pattern(1 0 1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Number\\(1)}" "\specialcell{IHS\\(2)}" ///
	"\specialcell{Number\\(3)}" "\specialcell{IHS\\(4)}" ///	
	"\specialcell{Number\\(5)}" "\specialcell{IHS\\(6)}")	

	// Output Heterogeneous Effects Table
	local tex "Neighbor $\times$ Different `Hetero' & `coef_1_out' & `coef_2_out' & `coef_Lead1_out' & `coef_Lead2_out' & `coef_Lag1_out' & `coef_Lag2_out' \\"
	local tex "`tex' `bipart' & [`pval_1_out']`pstar_1_out' & [`pval_2_out']`pstar_2_out' & [`pval_Lead1_out']`pstar_Lead1_out' & [`pval_Lead2_out']`pstar_Lead2_out' & [`pval_Lag1_out']`pstar_Lag1_out' & [`pval_Lag2_out']`pstar_Lag2_out' \\"
	local tex "`tex' & \{`pval_1_RIout'\}`pstar_1_RIout' & \{`pval_2_RIout'\}`pstar_2_RIout' & \{`pval_Lead1_RIout'\}`pstar_Lead1_RIout' & \{`pval_Lead2_RIout'\}`pstar_Lead2_RIout' & \{`pval_Lag1_RIout'\}`pstar_Lag1_RIout' & \{`pval_Lag2_RIout'\}`pstar_Lag2_RIout' \\"	
	local tex "`tex' & <`qval_1_out'>`qstar_1_out' & <`qval_2_out'>`qstar_2_out' & <`qval_Lead1_out'>`qstar_Lead1_out' & <`qval_Lead2_out'>`qstar_Lead2_out' & & \\ \\[-5pt]"
	
	local tex "`tex' Neighbor $\times$ Same `Hetero' & `coef_1_in' & `coef_2_in' & `coef_Lead1_in' & `coef_Lead2_in'  & `coef_Lag1_in' & `coef_Lag2_in' \\"
	local tex "`tex'  & [`pval_1_in']`pstar_1_in' & [`pval_2_in']`pstar_2_in' & [`pval_Lead1_in']`pstar_Lead1_in' & [`pval_Lead2_in']`pstar_Lead2_in' & [`pval_Lag1_in']`pstar_Lag1_in' & [`pval_Lag2_in']`pstar_Lag2_in' \\"
	local tex "`tex' & \{`pval_1_RIin'\}`pstar_1_RIin' & \{`pval_2_RIin'\}`pstar_2_RIin' & \{`pval_Lead1_RIin'\}`pstar_Lead1_RIin' & \{`pval_Lead2_RIin'\}`pstar_Lead2_RIin' & \{`pval_Lag1_RIin'\}`pstar_Lag1_RIin' & \{`pval_Lag2_RIin'\}`pstar_Lag2_RIin' \\"
	local tex "`tex' & <`qval_1_in'>`qstar_1_in' & <`qval_2_in'>`qstar_2_in' & <`qval_Lead1_in'>`qstar_Lead1_in' & <`qval_Lead2_in'>`qstar_Lead2_in' & & \\ \\[-5pt]"
	
		
	local tex "`tex' Same = Different & [`pval_1_diff']`pstar_1_diff' & [`pval_2_diff']`pstar_2_diff' & [`pval_Lead1_diff']`pstar_Lead1_diff' & [`pval_Lead2_diff']`pstar_Lead2_diff' & [`pval_Lag1_diff']`pstar_Lag1_diff' & [`pval_Lag2_diff']`pstar_Lag2_diff' \\"
	local tex "`tex' & \{`pval_1_RIdiff'\}`pstar_1_RIdiff' & \{`pval_2_RIdiff'\}`pstar_2_RIdiff' & \{`pval_Lead1_RIdiff'\}`pstar_Lead1_RIdiff' & \{`pval_Lead2_RIdiff'\}`pstar_Lead2_RIdiff' & \{`pval_Lag1_RIdiff'\}`pstar_Lag1_RIdiff' & \{`pval_Lag2_RIdiff'\}`pstar_Lag2_RIdiff' \\ \\[-5pt] \hline"				
	
	local tex "`tex' Observations & `obsHet1' & `obsHet2' & `obsHetLead1' & `obsHetLead2' & `obsHetLag1' & `obsHetLag2' \\" 
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `meanLead1' & `meanLead2' & `meanLag1' & `meanLag2' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sdLead1' & `sdLead2' & `sdLag1' & `sdLag2' \\ \toprule" 
	
	local tex "`tex' \multicolumn{7}{p{16.9cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `rawnumb' `ihsAll'"
	local tex "`tex' `neighbor' `samehet'"
	local tex "`tex' `dyadSquare' `riCurly' `qlab'"
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `plab'}"
	local tex "`tex' \\ \end{tabular} }"
	
	esttab Het1 Het2 HetLead1 HetLead2 HetLag1 HetLag2 using "`tablefolder'//pairCospon`Hetero'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("\specialcell{Contemporaneous\\Effect (t)}" "\specialcell{One Year\\Later (t+1)}" ///
			"\specialcell{Previous Year\\(Placebo) (t-1)}", pattern(1 0 1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Number\\(1)}" "\specialcell{IHS\\(2)}" ///
	"\specialcell{Number\\(3)}" "\specialcell{IHS\\(4)}" ///	
	"\specialcell{Number\\(5)}" "\specialcell{IHS\\(6)}")	
	
	timer off 5
	disp "DONE dyadCospon"
	timer list 5
	
	beep
}
if `dyadCosponCornerDiff'==1 { // Dyadic cosponsorship results (corner vs. middle)

	timer on 6
	forv k = 1/3 { 		// Time period?
		local time: word `k' of "Lag" "" "Lead"

		forv r = 1/2 {	// Type of voting/cosponsorship measure?
			local measure: word `r' of "omni_coany" "omni_ihs_coany"
				
				dis "`measure'`time'"
				use `datamod', clear
				keep session_id *coany* *RI* minMP maxMP pairStrata samecoalition sameparty ///
				samegender specialSession shortSession firstSession lastSession leadShortIncomplete	party_id party_id_d ///
				sessionXpartyPair
								
				keep if pairStrata==2 // no-one in dyad pre-assigned seat
				drop if sameparty==1 // keep only diff-party dyads
				
				if `k'==1 {
					drop if firstSession==1 
				}
				else if `k'==3 {
					drop if lastSession==1
					drop if leadShortIncomplete==1
				}	
				
				/* Hetero: corner vs. middle effect */
				
				// avg. effect for use in RI
				qui areg `measure'`time' neighborRI0, absorb(sessionXpartyPair)
				local coef_`time'`r'_av = _b[neighborRI0]
				
				// residualize
				qui areg `measure'`time' session_id#cornerRI0 if !mi(neighborRI0XcornerRI0) & !mi(neighborRI0XmiddleRI0) & !mi(`measure'`time'), absorb(sessionXpartyPair)
				local numN = e(N)
				local numDummy = e(df_m)+e(df_a) 
				predict yresid, residuals
				qui areg neighborRI0XcornerRI0 session_id#cornerRI0 if !mi(neighborRI0XcornerRI0) & !mi(neighborRI0XmiddleRI0) & !mi(`measure'`time'), absorb(sessionXpartyPair)
				local numN2 = e(N)
				predict xoutresid, residuals
				assert `numN' == `numN2'
				qui areg neighborRI0XmiddleRI0 session_id#cornerRI0 if !mi(neighborRI0XcornerRI0) & !mi(neighborRI0XmiddleRI0) & !mi(`measure'`time'), absorb(sessionXpartyPair)
				predict xinresid, residuals
				
				// run regression
				xi: regdyad2 yresid xinresid xoutresid if !mi(neighborRI0XcornerRI0) & !mi(neighborRI0XmiddleRI0) & !mi(`measure'`time') ///
					& !mi(party_id) & !mi(party_id_d), dyads(minMP maxMP)
				est sto Het`time'`r'
				local obsHet`time'`r' = e(N)
				local numReg = 2
				matrix varMatrix = e(V)
				
				// summary stats
				qui su `measure'`time' if e(sample)==1
				local mean`time'`r' = string(r(mean),"%9.2g")
				local sd`time'`r' = string(r(sd),"%9.2g")
				local sd`time'`r'raw = `r(sd)'
				
				// prepare coefficient, standard error, p-value for export					
				local coef_`time'`r'_in = string(_b[xinresid],"%9.2g")
				local coef_`time'`r'_out = string(_b[xoutresid],"%9.2g")
				local se_`time'`r'_in = _se[xinresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
				local se_`time'`r'_out = _se[xoutresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
				local t_df = `numN'-`numReg'-`numDummy'-1
				local pval_`time'`r'_in =  string(ttail(`t_df',abs(`coef_`time'`r'_in'/`se_`time'`r'_in'))*2,"%9.2g")
				local pval_`time'`r'_out =  string(ttail(`t_df',abs(`coef_`time'`r'_out'/`se_`time'`r'_out'))*2,"%9.2g")
				
				// pval calculation for the difference
				local diff = `coef_`time'`r'_in' - `coef_`time'`r'_out'
				local se_`time'`r'_diff = sqrt(varMatrix[1,1]+varMatrix[2,2]-2*varMatrix[2,1])
				local pval_`time'`r'_diff =  string(ttail(`t_df',abs(`diff'/`se_`time'`r'_diff'))*2,"%9.2g")
				drop yresid xinresid xoutresid
				
				/* diff-party point estimate and 95% confidence interval for figure
				if `k'==2 {
					local core_`r'_out = `coef_`r'_out'/`sd`r'raw'
					local corel_`r'_out = (`coef_`r'_out' - invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
					local coreu_`r'_out = (`coef_`r'_out' + invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
				}
				*/
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme_in = 0
					local Nbeta_extreme_out = 0
					local Nbeta_extreme_diff = 0
					forv i=1/`runsRI' {
						areg `measure'`time' neighborRI`i'XmiddleRI`i' neighborRI`i'XcornerRI`i' session_id#cornerRI`i', absorb(sessionXpartyPair)
						local absCoefIn = abs(_b[neighborRI`i'XmiddleRI`i'])
						local absCoefOut = abs(_b[neighborRI`i'XcornerRI`i'])
						local Nbeta_extreme_in = `Nbeta_extreme_in' + (`absCoefIn' >= abs(`coef_`time'`r'_in')) // + 1 every time see more extreme beta
						local Nbeta_extreme_out = `Nbeta_extreme_out' + (`absCoefOut' >= abs(`coef_`time'`r'_out')) 
					
						// for difference between middle vs corner p-value we need separate regressions, using "modified" outcome
						g `measure'`time'_m = `measure'`time'
						replace `measure'`time'_m = `measure'`time'_m + `coef_`time'`r'_av' if neighborRI`i' == 1 & neighborRI0 == 0
						replace `measure'`time'_m = `measure'`time'_m - `coef_`time'`r'_av' if neighborRI`i' == 0 & neighborRI0 == 1	
						
						// run regression with modified outcome
						areg `measure'`time'_m neighborRI`i'XmiddleRI`i' neighborRI`i'XcornerRI`i' session_id#cornerRI`i', absorb(sessionXpartyPair)
						local absCoefDiff = abs(_b[neighborRI`i'XmiddleRI`i']-_b[neighborRI`i'XcornerRI`i'])
						local Nbeta_extreme_diff = `Nbeta_extreme_diff' + (`absCoefDiff' >= abs(`diff')) 
						drop `measure'`time'_m
					}
					local pval_`time'`r'_RIin = string(`Nbeta_extreme_in'/`runsRI',"%9.2g")
					local pval_`time'`r'_RIout = string(`Nbeta_extreme_out'/`runsRI',"%9.2g")	
					local pval_`time'`r'_RIdiff = string(`Nbeta_extreme_diff'/`runsRI',"%9.2g")	
				}
				else {
					local pval_`time'`r'_RIin = 999
					local pval_`time'`r'_RIout = 999	
					local pval_`time'`r'_RIdiff = 999					
				}
				
				// get stars for all pvals
				foreach y in in out diff RIin RIout RIdiff {
					local pstar_`time'`r'_`y' = "" // reset p-value
					if `pval_`time'`r'_`y''<0.10 & `pval_`time'`r'_`y''>=0.05 local pstar_`time'`r'_`y' = "*"
					if `pval_`time'`r'_`y''<0.05 & `pval_`time'`r'_`y''>=0.01 local pstar_`time'`r'_`y' = "**"
					if `pval_`time'`r'_`y''<0.01 local pstar_`time'`r'_`y' = "***"
				}
				
				// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
				foreach y in in out diff {
					if `pval_`time'`r'_`y''<0.001 local pval_`time'`r'_`y' = "<0.001" 
				}					
		}
	}
	
	// Get sharpened q-values (Anderson 2008)	
	g float pval=.
	local i=1
	foreach x in pval_1_out pval_2_out pval_Lead1_out pval_Lead2_out ///
		pval_1_in pval_2_in pval_Lead1_in pval_Lead2_in {
		
		replace pval=``x'' in `i'
		local i=`i'+1
	}
	keep pval
	drop if mi(pval)
		
	run "$path_pch/Code/analysis/fdr_sharpened_qvalues.do"
	
	local i=1
	foreach x in qval_1_out qval_2_out qval_Lead1_out qval_Lead2_out ///
		qval_1_in qval_2_in qval_Lead1_in qval_Lead2_in {
		
		local `x' = string(qval,"%9.2g") in `i'
		if qval<0.001 in `i' local `x' = "<0.001" 
		
		local i=`i'+1
	}	
	
	// get stars for qvals
	local i=1
	foreach y in qstar_1_out qstar_2_out qstar_Lead1_out qstar_Lead2_out qstar_1_in qstar_2_in qstar_Lead1_in qstar_Lead2_in {
		local `y' = "" // reset q-star
		local j = qval in `i' // raw p-value
		
		if `j'<0.10 & `j'>=0.05 local `y' = "*"
		if `j'<0.05 & `j'>=0.01 local `y' = "**"
		if `j'<0.01 local `y' = "***"
				
		local i=`i'+1
	}	
			
	
	// Output Table
	local tex "Neighbor $\times$ Corner & `coef_1_out' & `coef_2_out' & `coef_Lead1_out' & `coef_Lead2_out' & `coef_Lag1_out' & `coef_Lag2_out' \\"
	local tex "`tex' & [`pval_1_out']`pstar_1_out' & [`pval_2_out']`pstar_2_out' & [`pval_Lead1_out']`pstar_Lead1_out' & [`pval_Lead2_out']`pstar_Lead2_out' & [`pval_Lag1_out']`pstar_Lag1_out' & [`pval_Lag2_out']`pstar_Lag2_out' \\"
	local tex "`tex' & \{`pval_1_RIout'\}`pstar_1_RIout' & \{`pval_2_RIout'\}`pstar_2_RIout' & \{`pval_Lead1_RIout'\}`pstar_Lead1_RIout' & \{`pval_Lead2_RIout'\}`pstar_Lead2_RIout' & \{`pval_Lag1_RIout'\}`pstar_Lag1_RIout' & \{`pval_Lag2_RIout'\}`pstar_Lag2_RIout' \\"	
	local tex "`tex' & <`qval_1_out'>`qstar_1_out' & <`qval_2_out'>`qstar_2_out' & <`qval_Lead1_out'>`qstar_Lead1_out' & <`qval_Lead2_out'>`qstar_Lead2_out' & & \\ \\[-5pt]"
	
	
	local tex "`tex' Neighbor $\times$ Middle & `coef_1_in' & `coef_2_in' & `coef_Lead1_in' & `coef_Lead2_in'  & `coef_Lag1_in' & `coef_Lag2_in' \\"
	local tex "`tex'  & [`pval_1_in']`pstar_1_in' & [`pval_2_in']`pstar_2_in' & [`pval_Lead1_in']`pstar_Lead1_in' & [`pval_Lead2_in']`pstar_Lead2_in' & [`pval_Lag1_in']`pstar_Lag1_in' & [`pval_Lag2_in']`pstar_Lag2_in' \\"
	local tex "`tex' & \{`pval_1_RIin'\}`pstar_1_RIin' & \{`pval_2_RIin'\}`pstar_2_RIin' & \{`pval_Lead1_RIin'\}`pstar_Lead1_RIin' & \{`pval_Lead2_RIin'\}`pstar_Lead2_RIin' & \{`pval_Lag1_RIin'\}`pstar_Lag1_RIin' & \{`pval_Lag2_RIin'\}`pstar_Lag2_RIin' \\"
	local tex "`tex' & <`qval_1_in'>`qstar_1_in' & <`qval_2_in'>`qstar_2_in' & <`qval_Lead1_in'>`qstar_Lead1_in' & <`qval_Lead2_in'>`qstar_Lead2_in' & & \\ \\[-5pt]"
		
	local tex "`tex' Corner = Middle & [`pval_1_diff']`pstar_1_diff' & [`pval_2_diff']`pstar_2_diff' & [`pval_Lead1_diff']`pstar_Lead1_diff' & [`pval_Lead2_diff']`pstar_Lead2_diff' & [`pval_Lag1_diff']`pstar_Lag1_diff' & [`pval_Lag2_diff']`pstar_Lag2_diff' \\"
	local tex "`tex' & \{`pval_1_RIdiff'\}`pstar_1_RIdiff' & \{`pval_2_RIdiff'\}`pstar_2_RIdiff' & \{`pval_Lead1_RIdiff'\}`pstar_Lead1_RIdiff' & \{`pval_Lead2_RIdiff'\}`pstar_Lead2_RIdiff' & \{`pval_Lag1_RIdiff'\}`pstar_Lag1_RIdiff' & \{`pval_Lag2_RIdiff'\}`pstar_Lag2_RIdiff' \\ \\[-5pt] \hline"				
	
	local tex "`tex' Observations & `obsHet1' & `obsHet2' & `obsHetLead1' & `obsHetLead2' & `obsHetLag1' & `obsHetLag2' \\" 
	local tex "`tex' Session $\times$ Corner FE & Y & Y & Y & Y & Y & Y \\"	
	local tex "`tex' Session $\times$ Party Pair FE & Y & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `meanLead1' & `meanLead2' & `meanLag1' & `meanLag2' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sdLead1' & `sdLead2' & `sdLag1' & `sdLag2' \\ \toprule" 
	
	local tex "`tex' \multicolumn{7}{p{14.5cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `diffPartyOnly'"
	local tex "`tex' `rawnumb' `ihsAll'"
	local tex "`tex' `neighbor' `corner'"
	local tex "`tex' `dyadSquare' `riCurly' `qlab'"
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `plab'}"
	local tex "`tex' \\ \end{tabular} }"
	
	esttab Het1 Het2 HetLead1 HetLead2 HetLag1 HetLag2 using "`tablefolder'//pairCosponCornerDiff.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("\specialcell{Contemporaneous\\Effect (t)}" "\specialcell{One Year\\Later (t+1)}" ///
			"\specialcell{Previous Year\\(Placebo) (t-1)}", pattern(1 0 1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Number\\(1)}" "\specialcell{IHS\\(2)}" ///
	"\specialcell{Number\\(3)}" "\specialcell{IHS\\(4)}" ///	
	"\specialcell{Number\\(5)}" "\specialcell{IHS\\(6)}")	
	
	timer off 6
	disp "DONE dyadCosponCornerDiff"
	timer list 6

}
if `mpCospon'==1 { // MP-session-level cosponsorship results

	timer on 7
	use "`votingfolder'/MP_session_analysis", clear
	
	if `reweight'==1 {
		bys strataSessionPartyFE: egen probHalf = mean(halfOther`Hetero'NeighborRI0)
		bys strataSessionPartyFE: egen probAll = mean(allOther`Hetero'NeighborRI0)
		g noOther`Hetero'NeighborRI0 = halfOther`Hetero'NeighborRI0==0 & allOther`Hetero'NeighborRI0==0 & !mi(halfOther`Hetero'NeighborRI0)
		bys strataSessionPartyFE: egen probNone = mean(noOther`Hetero'NeighborRI0)
		
		g invProb = 1/probHalf if halfOther`Hetero'NeighborRI0==1
		replace invProb = 1/probAll if allOther`Hetero'NeighborRI0==1
		replace invProb = 1/probNone if noOther`Hetero'NeighborRI0==1
	}		

	// Define sessions that we will use
	g sampleSession = (!specialSession) & (!shortSession) & !(session_id == 149)
	g sampleSessionLag = !inlist(session_id,115,119,120,124,125,129,130,134,135,137,138,142,143,146,147,148,149)
	g sampleSessionLead = !inlist(session_id,118,119,123,124,128,129,133,134,136,137,141,142,145,146,147,148,149)

	// Three panels: contemporary, lead, lag (placebo)
	forv k = 1/3 { 		// Time period?
		local time: word `k' of "Lag" "" "Lead"	
	
		forv r = 1/2 {	// Type of voting/cosponsorship measure?
			local measure: word `r' of "cospon_all" "ihs_cospon_all"			
			
			areg `measure'`time' fracOther`Hetero'NeighborRI0 if sampleSession`time' `wgt', absorb(strataSessionPartyFE) cluster(MP_id)
			est sto MP`time'`r'
			local obs`time'`r' = e(N)	
			
			// summary stats
			qui su `measure'`time' if e(sample)==1
			local mean`time'`r' = string(r(mean),"%9.2g")
			local sd`time'`r' = string(r(sd),"%9.2g")
			local sd`time'`r'raw = `r(sd)'	
			
			// prepare coefficient, standard error, p-value for export
			local coef_`time'`r'_av = _b[fracOther`Hetero'NeighborRI0]
			local se_`time'`r'_av = string(_se[fracOther`Hetero'NeighborRI0],"%9.2g")
			local pval_`time'`r'_av = string(ttail(e(df_r),abs(`coef_`time'`r'_av'/`se_`time'`r'_av'))*2,"%9.2g")	
			local pval_`time'`r'_av_raw = ttail(e(df_r),abs(`coef_`time'`r'_av'/`se_`time'`r'_av'))*2	
							
			// diff-party point estimate and 95% confidence interval for figure
			if `k'==2 {
				local mp_`r'_out = `coef_`r'_av'/`sd`r'raw'
				local mpl_`r'_out = (`coef_`r'_av' - invttail(e(df_r),0.025)*`se_`r'_av')/`sd`r'raw'
				local mpu_`r'_out = (`coef_`r'_av' + invttail(e(df_r),0.025)*`se_`r'_av')/`sd`r'raw'
			}		
			
			// randomization inference p-values
			if `RI'==1 {
				local Nbeta_extreme = 0
				forv i=1/`runsRI' {
					
					if `reweight'==1 {
						drop probHalf probAll probNone invProb
						
						bys strataSessionPartyFE: egen probHalf = mean(halfOther`Hetero'NeighborRI`i')
						bys strataSessionPartyFE: egen probAll = mean(allOther`Hetero'NeighborRI`i')
						g noOther`Hetero'NeighborRI`i' = halfOther`Hetero'NeighborRI`i'==0 & ///
								allOther`Hetero'NeighborRI`i'==0 & !mi(halfOther`Hetero'NeighborRI`i')
						bys strataSessionPartyFE: egen probNone = mean(noOther`Hetero'NeighborRI`i')
						
						g invProb = 1/probHalf if halfOther`Hetero'NeighborRI`i'==1
						replace invProb = 1/probAll if allOther`Hetero'NeighborRI`i'==1
						replace invProb = 1/probNone if noOther`Hetero'NeighborRI`i'==1
						
						drop noOther`Hetero'NeighborRI`i'						
					}
					
					areg `measure'`time' fracOther`Hetero'NeighborRI`i' if sampleSession`time' `wgt', absorb(strataSessionPartyFE)
					local absCoef = abs(_b[fracOther`Hetero'NeighborRI`i'])
					local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`time'`r'_av')) // + 1 every time see more extreme beta
				}
				local pval_`time'`r'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
			}
			else {
				local pval_`time'`r'_RI = 999
			}
			
			// get stars for all pvals
			foreach y in av RI {
				local pstar_`time'`r'_`y' = "" // reset p-value
				if `pval_`time'`r'_`y''<0.10 & `pval_`time'`r'_`y''>=0.05 local pstar_`time'`r'_`y' = "*"
				if `pval_`time'`r'_`y''<0.05 & `pval_`time'`r'_`y''>=0.01 local pstar_`time'`r'_`y' = "**"
				if `pval_`time'`r'_`y''<0.01 local pstar_`time'`r'_`y' = "***"
			}	
			
			// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
			if `pval_`time'`r'_av'<0.001 local pval_`time'`r'_av = "<0.001" 				
		}
	}
	
	// Get sharpened q-values (Anderson 2008)	
	g float pval=.
	local i=1
	foreach x in pval_1_av_raw pval_2_av_raw pval_Lead1_av_raw pval_Lead2_av_raw {
		
		replace pval=``x'' in `i'
		local i=`i'+1
	}
	keep pval
	drop if mi(pval)
		
	run "$path_pch/Code/analysis/fdr_sharpened_qvalues.do"
	
	local i=1
	foreach x in qval_1_av qval_2_av qval_Lead1_av qval_Lead2_av {
		
		local `x' = string(qval,"%9.2g") in `i'
		if qval<0.001 in `i' local `x' = "<0.001" 
		
		local i=`i'+1
	}	
	
	// get stars for qvals
	local i=1
	foreach y in qstar_1_av qstar_2_av qstar_Lead1_av qstar_Lead2_av {
		local `y' = "" // reset q-star
		local j = qval in `i' // raw p-value
		
		if `j'<0.10 & `j'>=0.05 local `y' = "*"
		if `j'<0.05 & `j'>=0.01 local `y' = "**"
		if `j'<0.01 local `y' = "***"
				
		local i=`i'+1
	}	
			
	
	// Output Main Effects Table
	estout MP1 MP2 MPLead1 MPLead2 MPLag1 MPLag2 using "`tablefolder'//mpCospon`Hetero'A`wgtlab'.tex", style(tex) replace ///
	keep(fracOther`Hetero'NeighborRI0) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(fracOther`Hetero'NeighborRI0 "Proportion Other-`Hetero' Neighbor")

	local tex "\input{mpCospon`Hetero'A`wgtlab'} \vspace{-1.2em} \\"
	local tex "`tex' & (`se_1_av') & (`se_2_av') & (`se_Lead1_av') & (`se_Lead2_av') & (`se_Lag1_av') & (`se_Lag2_av') \\"
	local tex "`tex' & [`pval_1_av']`pstar_1_av' & [`pval_2_av']`pstar_2_av' & [`pval_Lead1_av']`pstar_Lead1_av' & [`pval_Lead2_av']`pstar_Lead2_av' & [`pval_Lag1_av']`pstar_Lag1_av' & [`pval_Lag2_av']`pstar_Lag2_av' \\"
	local tex "`tex' & \{`pval_1_RI'\}`pstar_1_RI' & \{`pval_2_RI'\}`pstar_2_RI' &  \{`pval_Lead1_RI'\}`pstar_Lead1_RI' & \{`pval_Lead2_RI'\}`pstar_Lead2_RI' & \{`pval_Lag1_RI'\}`pstar_Lag1_RI' & \{`pval_Lag2_RI'\}`pstar_Lag2_RI' \\"	
	local tex "`tex' & <`qval_1_av'>`qstar_1_av' & <`qval_2_av'>`qstar_2_av' & <`qval_Lead1_av'>`qstar_Lead1_av' & <`qval_Lead2_av'>`qstar_Lead2_av' & & \\ \\[-5pt] \hline"
	
	local tex "`tex' Observations & `obs1' & `obs2' & `obsLead1' & `obsLead2' & `obsLag1' & `obsLag2' \\"
	local tex "`tex' Session $\times$ Party $\times$ Strata FE & Y & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `meanLead1' & `meanLead2' & `meanLag1' & `meanLag2' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sdLead1' & `sdLead2' & `sdLag1' & `sdLag2' \\ \toprule" 
	
	local tex "`tex' \multicolumn{7}{p{16cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `wgttabnote'`rawnumbMP' `ihsAll' `propothMP'"
	local tex "`tex' `mpSEp' `riCurly'"
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDefMP' `plab'}"	
	local tex "`tex' \\ \end{tabular} }"
	
	esttab MP1 MP2 MPLead1 MPLead2 MPLag1 MPLag2 using "`tablefolder'//mpCospon`Hetero'`wgtlab'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("\specialcell{Contemporaneous\\Effect (t)}" "\specialcell{One Year\\Later (t+1)}" ///
			"\specialcell{Previous Year\\(Placebo) (t-1)}", pattern(1 0 1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Number\\(1)}" "\specialcell{IHS\\(2)}" ///
	"\specialcell{Number\\(3)}" "\specialcell{IHS\\(4)}" ///	
	"\specialcell{Number\\(5)}" "\specialcell{IHS\\(6)}")		
	
	timer off 7
	disp "DONE mpCospon"
	timer list 7

}
if `dyadVoteYesNo'==1 { // Dyadic voting results (yes-yes, no-no)

	timer on 8
	forv k = 1/3 { 		// Time period?
		local time: word `k' of "Lag" "" "Lead"

		forv r = 1/4 {	// Type of voting/cosponsorship measure?
			local measure: word `r' of "samevote4c" "samevotesc" "samevotesc_VS" "samevotesc_VVS"
				
				dis "`measure'`time'"
				use `datamod', clear
				keep session_id samevote?c* votediff?c* voteSim?c* *RI* minMP maxMP pairStrata samecoalition sameparty ///
				samegender specialSession shortSession firstSession lastSession leadShortIncomplete	party_id party_id_d ///
				sessionXpairStrataXpartyPair
				
				if `k'==1 {
					drop if firstSession==1 
				}
				else if `k'==3 {
					drop if lastSession==1
					drop if leadShortIncomplete==1
				}
				
				/* Neighborhood effect */
				
				// residualize
				qui areg `measure'`time' if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair)
				local numN = e(N)
				local numDummy = e(df_a) 
				predict yresid, residuals
				qui areg neighborRI0 if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
				local numN2 = e(N)
				predict xresid, residuals
				assert `numN' == `numN2'
				
				// run regression
				xi: regdyad2 yresid xresid if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d), ///
					dyads(minMP maxMP)
				est sto M`time'`r'
				local obs`time'`r' = e(N)
				qui su `measure'`time' if e(sample)==1
				local mean`time'`r' = string(r(mean),"%9.2g")
				local sd`time'`r' = string(r(sd),"%9.2g")
				local sd`time'`r'raw = `r(sd)'
				local numReg = 1
				
				// prepare coefficient, standard error, p-value for export
				local coef_`time'`r'_av = _b[xresid]
				local se_`time'`r'_av = string(_se[xresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1)),"%9.2g")
				local t_df = `numN'-`numReg'-`numDummy'-1
				local pval_`time'`r'_av = string(ttail(`t_df',abs(`coef_`time'`r'_av'/`se_`time'`r'_av'))*2,"%9.2g")					
				drop yresid xresid	
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme = 0
					forv i=1/`runsRI' {
						areg `measure'`time' neighborRI`i', absorb(sessionXpairStrataXpartyPair)
						local absCoef = abs(_b[neighborRI`i'])
						local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`time'`r'_av')) // + 1 every time see more extreme beta
					}
					local pval_`time'`r'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
				}
				else {
					local pval_`time'`r'_RI = 999
				}					
				
				/* Hetero: party ingroup and outgroup effect */
								
				// residualize
				qui areg `measure'`time' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure'`time') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair)
				local numN = e(N)
				local numDummy = e(df_a) 
				predict yresid, residuals
				qui areg neighborRI0Xsame`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure'`time') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
				local numN2 = e(N)
				predict xinresid, residuals
				assert `numN' == `numN2'
				qui areg neighborRI0Xdiff`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure'`time') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
				predict xoutresid, residuals
				
				// run regression
				xi: regdyad2 yresid xinresid xoutresid if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure'`time') ///
					& !mi(pairStrata) & !mi(party_id) & !mi(party_id_d), dyads(minMP maxMP)
				est sto Het`time'`r'
				local obsHet`time'`r' = e(N)
				local numReg = 2
				matrix varMatrix = e(V)
				
				// prepare coefficient, standard error, p-value for export					
				local coef_`time'`r'_in = string(_b[xinresid],"%9.2g")
				local coef_`time'`r'_out = string(_b[xoutresid],"%9.2g")
				local se_`time'`r'_in = _se[xinresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
				local se_`time'`r'_out = _se[xoutresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
				local t_df = `numN'-`numReg'-`numDummy'-1
				local pval_`time'`r'_in =  string(ttail(`t_df',abs(`coef_`time'`r'_in'/`se_`time'`r'_in'))*2,"%9.2g")
				local pval_`time'`r'_out =  string(ttail(`t_df',abs(`coef_`time'`r'_out'/`se_`time'`r'_out'))*2,"%9.2g")
				
				// pval calculation for the difference
				local diff = `coef_`time'`r'_in' - `coef_`time'`r'_out'
				local se_`time'`r'_diff = sqrt(varMatrix[1,1]+varMatrix[2,2]-2*varMatrix[2,1])
				local pval_`time'`r'_diff =  string(ttail(`t_df',abs(`diff'/`se_`time'`r'_diff'))*2,"%9.2g")
				drop yresid xinresid xoutresid
				
				/*
				// diff-party point estimate and 95% confidence interval for figure
				if `k'==2 {
					local voteYesNo_`r'_out = `coef_`r'_out'/`sd`r'raw'
					local voteYesNol_`r'_out = (`coef_`r'_out' - invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
					local voteYesNou_`r'_out = (`coef_`r'_out' + invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
				}
				*/
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme_in = 0
					local Nbeta_extreme_out = 0
					local Nbeta_extreme_diff = 0
					forv i=1/`runsRI' {
						areg `measure'`time' neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero', absorb(sessionXpairStrataXpartyPair)
						local absCoefIn = abs(_b[neighborRI`i'Xsame`hetero'])
						local absCoefOut = abs(_b[neighborRI`i'Xdiff`hetero'])
						local Nbeta_extreme_in = `Nbeta_extreme_in' + (`absCoefIn' >= abs(`coef_`time'`r'_in')) // + 1 every time see more extreme beta
						local Nbeta_extreme_out = `Nbeta_extreme_out' + (`absCoefOut' >= abs(`coef_`time'`r'_out')) 
					
						// for difference between in vs out p-value we need separate regressions, using "modified" outcome
						g `measure'`time'_m = `measure'`time'
						replace `measure'`time'_m = `measure'`time'_m + `coef_`time'`r'_av' if neighborRI`i' == 1 & neighborRI0 == 0
						replace `measure'`time'_m = `measure'`time'_m - `coef_`time'`r'_av' if neighborRI`i' == 0 & neighborRI0 == 1	
						
						// run regression with modified outcome
						areg `measure'`time'_m neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero', absorb(sessionXpairStrataXpartyPair)
						local absCoefDiff = abs(_b[neighborRI`i'Xsame`hetero']-_b[neighborRI`i'Xdiff`hetero'])
						local Nbeta_extreme_diff = `Nbeta_extreme_diff' + (`absCoefDiff' >= abs(`diff')) 
						drop `measure'`time'_m
					}
					local pval_`time'`r'_RIin = string(`Nbeta_extreme_in'/`runsRI',"%9.2g")
					local pval_`time'`r'_RIout = string(`Nbeta_extreme_out'/`runsRI',"%9.2g")	
					local pval_`time'`r'_RIdiff = string(`Nbeta_extreme_diff'/`runsRI',"%9.2g")	
				}
				else {
					local pval_`time'`r'_RIin = 999
					local pval_`time'`r'_RIout = 999	
					local pval_`time'`r'_RIdiff = 999										
				}								
				
				// get stars for all pvals
				foreach y in av in out diff RI RIin RIout RIdiff {
					local pstar_`time'`r'_`y' = "" // reset p-value
					if `pval_`time'`r'_`y''<0.10 & `pval_`time'`r'_`y''>=0.05 local pstar_`time'`r'_`y' = "*"
					if `pval_`time'`r'_`y''<0.05 & `pval_`time'`r'_`y''>=0.01 local pstar_`time'`r'_`y' = "**"
					if `pval_`time'`r'_`y''<0.01 local pstar_`time'`r'_`y' = "***"
				}
				
				// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
				foreach y in av in out diff {
					if `pval_`time'`r'_`y''<0.001 local pval_`time'`r'_`y' = "<0.001" 
				}					
		}
	}

		
	// Output Main Effects Table (online appendix)
	estout M1 M2 M3 M4 using "`tablefolder'//pairVoteYesNoA.tex", style(tex) replace ///
	keep(xresid) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(xresid "Neighbor")

	estout MLead1 MLead2 MLead3 MLead4 using "`tablefolder'//pairVoteYesNoB.tex", style(tex) replace ///
	keep(xresid) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(xresid "Neighbor")

	estout MLag1 MLag2 MLag3 MLag4 using "`tablefolder'//pairVoteYesNoC.tex", style(tex) replace ///
	keep(xresid) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(xresid "Neighbor")

	local tex "& \multicolumn{4}{c}{\textit{Panel A:} Contemporaneous Effect (t)} \vspace{0.2em} \\"
	local tex "`tex' \input{pairVoteYesNoA} \vspace{-1.2em} \\"
	local tex "`tex' & (`se_1_av') & (`se_2_av') & (`se_3_av') & (`se_4_av') \\"
	local tex "`tex' & [`pval_1_av']`pstar_1_av' & [`pval_2_av']`pstar_2_av' & [`pval_3_av']`pstar_3_av' & [`pval_4_av']`pstar_4_av' \\"
	local tex "`tex' & \{`pval_1_RI'\}`pstar_1_RI' & \{`pval_2_RI'\}`pstar_2_RI' & \{`pval_3_RI'\}`pstar_3_RI' & \{`pval_4_RI'\}`pstar_4_RI' \\ \\[-5pt]"	
	local tex "`tex' Observations & `obs1' & `obs2' & `obs3' & `obs4' \\ \hline"
	local tex "`tex' \vspace{-8pt} & & & & \\"
	local tex "`tex' & \multicolumn{4}{c}{\textit{Panel B:} One Year Later (t+1)} \vspace{0.2em} \\"  
	local tex "`tex' \input{pairVoteYesNoB} \vspace{-1.2em} \\" 
	local tex "`tex'  & (`se_Lead1_av') & (`se_Lead2_av') & (`se_Lead3_av') & (`se_Lead4_av') \\"
	local tex "`tex'  & [`pval_Lead1_av']`pstar_Lead1_av' & [`pval_Lead2_av']`pstar_Lead2_av' & [`pval_Lead3_av']`pstar_Lead3_av' & [`pval_Lead4_av']`pstar_Lead4_av' \\"
	local tex "`tex' & \{`pval_Lead1_RI'\}`pstar_Lead1_RI' & \{`pval_Lead2_RI'\}`pstar_Lead2_RI' & \{`pval_Lead3_RI'\}`pstar_Lead3_RI' & \{`pval_Lead4_RI'\}`pstar_Lead4_RI' \\ \\[-5pt]"		
	local tex "`tex' Observations & `obsLead1' & `obsLead2' & `obsLead3' & `obsLead4' \\ \hline" 
	local tex "`tex' \vspace{-8pt} & & & & \\"
	local tex "`tex' & \multicolumn{4}{c}{\textit{Panel C:} Previous Year (Placebo) (t-1)} \vspace{0.2em} \\"  
	local tex "`tex' \input{pairVoteYesNoC} \vspace{-1.2em} \\"
	local tex "`tex'  & (`se_Lag1_av') & (`se_Lag2_av') & (`se_Lag3_av') & (`se_Lag4_av') \\"
	local tex "`tex'  & [`pval_Lag1_av']`pstar_Lag1_av' & [`pval_Lag2_av']`pstar_Lag2_av' & [`pval_Lag3_av']`pstar_Lag3_av' & [`pval_Lag4_av']`pstar_Lag4_av' \\"
	local tex "`tex' & \{`pval_Lag1_RI'\}`pstar_Lag1_RI' & \{`pval_Lag2_RI'\}`pstar_Lag2_RI' & \{`pval_Lag3_RI'\}`pstar_Lag3_RI' & \{`pval_Lag4_RI'\}`pstar_Lag4_RI' \\ \\[-5pt]"			
	local tex "`tex' Observations & `obsLag1' & `obsLag2' & `obsLag3' & `obsLag4' \\ \hline" 	
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `mean3' & `mean4' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sd3' & `sd4' \\ \toprule" 
	local tex "`tex' \multicolumn{5}{p{14.9cm}}{\footnotesize \textit{Notes:} `eachPanelFour'" 
	local tex "`tex' `compliance' `yesyes' `contested' `neighbor'"
	local tex "`tex' `dyadSEp' `riCurly'"	
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `outcomeMeanA' `plab'}" 
	local tex "`tex' \\ \end{tabular} }"
	
	esttab M1 M2 M3 M4 using "`tablefolder'//pairVoteYesNo.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("Compliance" "Yes-Yes/No-No", pattern(1 1 0 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{All\\(1)}" "\specialcell{All\\(2)}" ///
	"\specialcell{Below 50th\\(3)}" "\specialcell{Below 25th\\(4)}")	

	// Output Heterogeneous Effects Table
	local tex "& \multicolumn{4}{c}{\textit{Panel A:} Contemporaneous Effect (t)} \vspace{0.2em} \\" 
	
	local tex "`tex' Neighbor $\times$ Different `Hetero' & `coef_1_out' & `coef_2_out' & `coef_3_out' & `coef_4_out' \\"
	local tex "`tex' `bipart' & [`pval_1_out']`pstar_1_out' & [`pval_2_out']`pstar_2_out' & [`pval_3_out']`pstar_3_out' & [`pval_4_out']`pstar_4_out' \\"
	local tex "`tex' & \{`pval_1_RIout'\}`pstar_1_RIout' & \{`pval_2_RIout'\}`pstar_2_RIout' & \{`pval_3_RIout'\}`pstar_3_RIout' & \{`pval_4_RIout'\}`pstar_4_RIout' \\ \\[-5pt]"				
	
	local tex "`tex' Neighbor $\times$ Same `Hetero' & `coef_1_in' & `coef_2_in' & `coef_3_in' & `coef_4_in' \\"
	local tex "`tex'  & [`pval_1_in']`pstar_1_in' & [`pval_2_in']`pstar_2_in' & [`pval_3_in']`pstar_3_in' & [`pval_4_in']`pstar_4_in' \\"
	local tex "`tex' & \{`pval_1_RIin'\}`pstar_1_RIin' & \{`pval_2_RIin'\}`pstar_2_RIin' & \{`pval_3_RIin'\}`pstar_3_RIin' & \{`pval_4_RIin'\}`pstar_4_RIin' \\ \\[-5pt]"		
	
	*local tex "`tex' Same = Different & [`pval_1_diff']`pstar_1_diff' & [`pval_2_diff']`pstar_2_diff' & [`pval_3_diff']`pstar_3_diff' & [`pval_4_diff']`pstar_4_diff' \\"
	*local tex "`tex' & \{`pval_1_RIdiff'\}`pstar_1_RIdiff' & \{`pval_2_RIdiff'\}`pstar_2_RIdiff' & \{`pval_3_RIdiff'\}`pstar_3_RIdiff' & \{`pval_4_RIdiff'\}`pstar_4_RIdiff' \\"				
	local tex "`tex' Observations & `obsHet1' & `obsHet2' & `obsHet3' & `obsHet4' \\ \hline" 
	local tex "`tex' \vspace{-8pt} & & & & \\"
	
	local tex "`tex' & \multicolumn{4}{c}{\textit{Panel B:} One Year Later (t+1)} \vspace{0.2em} \\"  

	local tex "`tex' Neighbor $\times$ Different `Hetero' & `coef_Lead1_out' & `coef_Lead2_out' & `coef_Lead3_out' & `coef_Lead4_out' \\"
	local tex "`tex' `bipart' & [`pval_Lead1_out']`pstar_Lead1_out' & [`pval_Lead2_out']`pstar_Lead2_out' & [`pval_Lead3_out']`pstar_Lead3_out' & [`pval_Lead4_out']`pstar_Lead4_out' \\"
	local tex "`tex' & \{`pval_Lead1_RIout'\}`pstar_Lead1_RIout' & \{`pval_Lead2_RIout'\}`pstar_Lead2_RIout' & \{`pval_Lead3_RIout'\}`pstar_Lead3_RIout' & \{`pval_Lead4_RIout'\}`pstar_Lead4_RIout' \\ \\[-5pt]"				
		
	local tex "`tex' Neighbor $\times$ Same `Hetero' & `coef_Lead1_in' & `coef_Lead2_in' & `coef_Lead3_in' & `coef_Lead4_in' \\"
	local tex "`tex'  & [`pval_Lead1_in']`pstar_Lead1_in' & [`pval_Lead2_in']`pstar_Lead2_in' & [`pval_Lead3_in']`pstar_Lead3_in' & [`pval_Lead4_in']`pstar_Lead4_in' \\"
	local tex "`tex' & \{`pval_Lead1_RIin'\}`pstar_Lead1_RIin' & \{`pval_Lead2_RIin'\}`pstar_Lead2_RIin' & \{`pval_Lead3_RIin'\}`pstar_Lead3_RIin' & \{`pval_Lead4_RIin'\}`pstar_Lead4_RIin' \\ \\[-5pt]"			
	
	*local tex "`tex' Same = Different & [`pval_Lead1_diff']`pstar_Lead1_diff' & [`pval_Lead2_diff']`pstar_Lead2_diff' & [`pval_Lead3_diff']`pstar_Lead3_diff' & [`pval_Lead4_diff']`pstar_Lead4_diff' \\"
	*local tex "`tex' & \{`pval_Lead1_RIdiff'\}`pstar_Lead1_RIdiff' & \{`pval_Lead2_RIdiff'\}`pstar_Lead2_RIdiff' & \{`pval_Lead3_RIdiff'\}`pstar_Lead3_RIdiff' & \{`pval_Lead4_RIdiff'\}`pstar_Lead4_RIdiff' \\"				
	local tex "`tex' Observations & `obsHetLead1' & `obsHetLead2' & `obsHetLead3' & `obsHetLead4' \\ \hline" 
	local tex "`tex' \vspace{-8pt} & & & & \\"
	
	local tex "`tex' & \multicolumn{4}{c}{\textit{Panel C:} Previous Year (Placebo) (t-1)} \vspace{0.2em} \\"  
	
	local tex "`tex' Neighbor $\times$ Different `Hetero' & `coef_Lag1_out' & `coef_Lag2_out' & `coef_Lag3_out' & `coef_Lag4_out' \\"
	local tex "`tex' `bipart' & [`pval_Lag1_out']`pstar_Lag1_out' & [`pval_Lag2_out']`pstar_Lag2_out' & [`pval_Lag3_out']`pstar_Lag3_out' & [`pval_Lag4_out']`pstar_Lag4_out' \\"
	local tex "`tex' & \{`pval_Lag1_RIout'\}`pstar_Lag1_RIout' & \{`pval_Lag2_RIout'\}`pstar_Lag2_RIout' & \{`pval_Lag3_RIout'\}`pstar_Lag3_RIout' & \{`pval_Lag4_RIout'\}`pstar_Lag4_RIout' \\ \\[-5pt]"				
		
	local tex "`tex' Neighbor $\times$ Same `Hetero' & `coef_Lag1_in' & `coef_Lag2_in' & `coef_Lag3_in' & `coef_Lag4_in' \\"
	local tex "`tex'  & [`pval_Lag1_in']`pstar_Lag1_in' & [`pval_Lag2_in']`pstar_Lag2_in' & [`pval_Lag3_in']`pstar_Lag3_in' & [`pval_Lag4_in']`pstar_Lag4_in' \\"
	local tex "`tex' & \{`pval_Lag1_RIin'\}`pstar_Lag1_RIin' & \{`pval_Lag2_RIin'\}`pstar_Lag2_RIin' & \{`pval_Lag3_RIin'\}`pstar_Lag3_RIin' & \{`pval_Lag4_RIin'\}`pstar_Lag4_RIin' \\ \\[-5pt]"			
	
	*local tex "`tex' Same = Different & [`pval_Lag1_diff']`pstar_Lag1_diff' & [`pval_Lag2_diff']`pstar_Lag2_diff' & [`pval_Lag3_diff']`pstar_Lag3_diff' & [`pval_Lag4_diff']`pstar_Lag4_diff' \\"
	*local tex "`tex' & \{`pval_Lag1_RIdiff'\}`pstar_Lag1_RIdiff' & \{`pval_Lag2_RIdiff'\}`pstar_Lag2_RIdiff' & \{`pval_Lag3_RIdiff'\}`pstar_Lag3_RIdiff' & \{`pval_Lag4_RIdiff'\}`pstar_Lag4_RIdiff' \\"				
	
	local tex "`tex' Observations & `obsHetLag1' & `obsHetLag2' & `obsHetLag3' & `obsHetLag4' \\ \hline" 	
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `mean3' & `mean4' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sd3' & `sd4' \\ \toprule" 
	local tex "`tex' \multicolumn{5}{p{15.4cm}}{\footnotesize \textit{Notes:} `eachPanelFour'" 
	local tex "`tex' `compliance' `yesyes' `contested' `neighbor' `samehet'"
	local tex "`tex' `dyadSquare' `riCurly'"	
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `outcomeMeanA' `plab'}" 
	local tex "`tex' \\ \end{tabular} }"
	
	esttab Het1 Het2 Het3 Het4 using "`tablefolder'//pairVoteYesNo`Hetero'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("Compliance" "Yes-Yes/No-No", pattern(1 1 0 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{All\\(1)}" "\specialcell{All\\(2)}" ///
	"\specialcell{Below 50th\\(3)}" "\specialcell{Below 25th\\(4)}")	
	
	timer off 8
	disp "DONE dyadYesNo"
	timer list 8
}
if `mpVoteOneTwo'==1 { // MP-session-level voting results (by intensity of contact)

	timer on 9
	use "`votingfolder'/MP_session_analysis", clear

	// Define sessions that we will use
	g sampleSession = (!specialSession) & (!shortSession) & !(session_id == 149)
	g sampleSessionLag = !inlist(session_id,115,119,120,124,125,129,130,134,135,137,138,142,143,146,147,148,149)
	g sampleSessionLead = !inlist(session_id,118,119,123,124,128,129,133,134,136,137,141,142,145,146,147,148,149)

	// Three panels: contemporary, lead, lag (placebo)
	forv k = 1/3 { 		// Time period?
		local time: word `k' of "Lag" "" "Lead"	
	
		forv r = 1/2 {	// Type of voting/cosponsorship measure?
			local measure: word `r' of "non_compliance" "rebelWeak"		
			areg `measure'`time' halfOther`Hetero'NeighborRI0 allOther`Hetero'NeighborRI0 if sampleSession`time', absorb(strataSessionPartyFE) cluster(MP_id)
			est sto MP`time'`r'
			local obs`time'`r' = e(N)	
			
			// summary stats
			qui su `measure'`time' if e(sample)==1
			local mean`time'`r' = string(r(mean),"%9.2g")
			local sd`time'`r' = string(r(sd),"%9.2g")
			local sd`time'`r'raw = `r(sd)'	
			
			// prepare coefficient, standard error, p-value for export
			foreach x in half all {
				local coef_`time'`r'_`x' = string(_b[`x'Other`Hetero'NeighborRI0],"%9.2g")
				local se_`time'`r'_`x' = string(_se[`x'Other`Hetero'NeighborRI0],"%9.2g")
				local pval_`time'`r'_`x' = string(ttail(e(df_r),abs(`coef_`time'`r'_`x''/`se_`time'`r'_`x''))*2,"%9.2g")	
			}	
				
			// diff-party point estimate and 95% confidence interval for figure
			/*
			if `k'==2 {
				local mp_`r'_out = `coef_`r'_av'/`sd`r'raw'
				local mpl_`r'_out = (`coef_`r'_av' - invttail(e(df_r),0.025)*`se_`r'_av')/`sd`r'raw'
				local mpu_`r'_out = (`coef_`r'_av' + invttail(e(df_r),0.025)*`se_`r'_av')/`sd`r'raw'
			}
			*/
			
			// randomization inference p-values
			if `RI'==1 {
				local Nbeta_extreme_half = 0
				local Nbeta_extreme_all = 0
				forv i=1/`runsRI' {
					areg `measure'`time' halfOther`Hetero'NeighborRI`i' allOther`Hetero'NeighborRI`i' if sampleSession`time', absorb(strataSessionPartyFE)
					
					foreach x in half all {
						local absCoef_`x' = abs(_b[`x'Other`Hetero'NeighborRI`i'])
						local Nbeta_extreme_`x' = `Nbeta_extreme_`x'' + (`absCoef_`x'' >= abs(`coef_`time'`r'_`x'')) // + 1 every time see more extreme beta
					}
				}
				local pval_`time'`r'_RI_half = string(`Nbeta_extreme_half'/`runsRI',"%9.2g")
				local pval_`time'`r'_RI_all = string(`Nbeta_extreme_all'/`runsRI',"%9.2g")	
			}
			else {
				local pval_`time'`r'_RI_half = 999
				local pval_`time'`r'_RI_all = 999				
			}
			
			// get stars for all pvals
			foreach y in half all RI_half RI_all {
				local pstar_`time'`r'_`y' = "" // reset p-value
				if `pval_`time'`r'_`y''<0.10 & `pval_`time'`r'_`y''>=0.05 local pstar_`time'`r'_`y' = "*"
				if `pval_`time'`r'_`y''<0.05 & `pval_`time'`r'_`y''>=0.01 local pstar_`time'`r'_`y' = "**"
				if `pval_`time'`r'_`y''<0.01 local pstar_`time'`r'_`y' = "***"
			}		
			
			// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
			foreach y in half all {
				if `pval_`time'`r'_`y''<0.001 local pval_`time'`r'_`y' = "<0.001" 
			}				
		}
	}
	
	// Output Heterogeneous Effects Table
	local tex "Proportion Other-`Hetero' Neighbor = 1/2 & `coef_1_half' & `coef_2_half' & `coef_Lead1_half' & `coef_Lead2_half' & `coef_Lag1_half' & `coef_Lag2_half' \\"
	local tex "`tex' & [`pval_1_half']`pstar_1_half' & [`pval_2_half']`pstar_2_half' & [`pval_Lead1_half']`pstar_Lead1_half' & [`pval_Lead2_half']`pstar_Lead2_half' & [`pval_Lag1_half']`pstar_Lag1_half' & [`pval_Lag2_half']`pstar_Lag2_half' \\"
	local tex "`tex' & \{`pval_1_RI_half'\}`pstar_1_RI_half' & \{`pval_2_RI_half'\}`pstar_2_RI_half' & \{`pval_Lead1_RI_half'\}`pstar_Lead1_RI_half' & \{`pval_Lead2_RI_half'\}`pstar_Lead2_RI_half' & \{`pval_Lag1_RI_half'\}`pstar_Lag1_RI_half' & \{`pval_Lag2_RI_half'\}`pstar_Lag2_RI_half' \\ \\[-5pt]"		
	
	local tex "`tex' Proportion Other-`Hetero' Neighbor = 1 & `coef_1_all' & `coef_2_all' & `coef_Lead1_all' & `coef_Lead2_all'  & `coef_Lag1_all' & `coef_Lag2_all' \\"
	local tex "`tex'  & [`pval_1_all']`pstar_1_all' & [`pval_2_all']`pstar_2_all' & [`pval_Lead1_all']`pstar_Lead1_all' & [`pval_Lead2_all']`pstar_Lead2_all' & [`pval_Lag1_all']`pstar_Lag1_all' & [`pval_Lag2_all']`pstar_Lag2_all' \\"
	local tex "`tex' & \{`pval_1_RI_all'\}`pstar_1_RI_all' & \{`pval_2_RI_all'\}`pstar_2_RI_all' & \{`pval_Lead1_RI_all'\}`pstar_Lead1_RI_all' & \{`pval_Lead2_RI_all'\}`pstar_Lead2_RI_all' & \{`pval_Lag1_RI_all'\}`pstar_Lag1_RI_all' & \{`pval_Lag2_RI_all'\}`pstar_Lag2_RI_all' \\ \\[-5pt] \hline"		
		
	local tex "`tex' Observations & `obs1' & `obs2' & `obsLead1' & `obsLead2' & `obsLag1' & `obsLag2' \\" 
	local tex "`tex' Session $\times$ Party $\times$ Strata FE & Y & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `meanLead1' & `meanLead2' & `meanLag1' & `meanLag2' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sdLead1' & `sdLead2' & `sdLag1' & `sdLag2' \\ \toprule" 
	
	local tex "`tex' \multicolumn{7}{p{19cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `noncomplianceMP' `rebelWeakMP'"
	local tex "`tex' `propothMP' `mpSquare' `riCurly'"
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDefMP' `plab'}"
	local tex "`tex' \\ \end{tabular} }"
	
	esttab MP1 MP2 MPLead1 MPLead2 MPLag1 MPLag2 using "`tablefolder'//mpVoteOneTwo`Hetero'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("\specialcell{Contemporaneous\\Effect (t)}" "\specialcell{One Year\\Later (t+1)}" ///
			"\specialcell{Previous Year\\(Placebo) (t-1)}", pattern(1 0 1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Leader\\Non-\\Compliance\\(1)}" "\specialcell{\\Rebel\\Rate\\(2)}" ///
	"\specialcell{Leader\\Non-\\Compliance\\(3)}" "\specialcell{\\Rebel\\Rate\\(4)}" ///	
	"\specialcell{Leader\\Non-\\Compliance\\(5)}" "\specialcell{\\Rebel\\Rate\\(6)}")		
	
	timer off 9
	disp "DONE mpVoteOneTwo"
	timer list 9

}
if `mpVoteContest'==1 { // MP-session-level voting results (contested)

	timer on 10
	use "`votingfolder'/MP_session_analysis", clear

	// Define sessions that we will use
	g sampleSession = (!specialSession) & (!shortSession) & !(session_id == 149)
	g sampleSessionLag = !inlist(session_id,115,119,120,124,125,129,130,134,135,137,138,142,143,146,147,148,149)
	g sampleSessionLead = !inlist(session_id,118,119,123,124,128,129,133,134,136,137,141,142,145,146,147,148,149)

	// Three panels: contemporary, lead, lag (placebo)
	forv k = 1/3 { 		// Time period?
		local time: word `k' of "Lag" "" "Lead"	
	
		forv r = 1/4 {	// Type of voting/cosponsorship measure?
			local measure: word `r' of "non_compliance_VS" "rebelWeak_VS" "non_compliance_VVS" "rebelWeak_VVS"				
			areg `measure'`time' fracOther`Hetero'NeighborRI0 if sampleSession`time', absorb(strataSessionPartyFE) cluster(MP_id)
			est sto MP`time'`r'
			local obs`time'`r' = e(N)	
			
			// summary stats
			qui su `measure'`time' if e(sample)==1
			local mean`time'`r' = string(r(mean),"%9.2g")
			local sd`time'`r' = string(r(sd),"%9.2g")
			local sd`time'`r'raw = `r(sd)'	
			
			// prepare coefficient, standard error, p-value for export
			local coef_`time'`r'_av = _b[fracOther`Hetero'NeighborRI0]
			local se_`time'`r'_av = string(_se[fracOther`Hetero'NeighborRI0],"%9.2g")
			local pval_`time'`r'_av = string(ttail(e(df_r),abs(`coef_`time'`r'_av'/`se_`time'`r'_av'))*2,"%9.2g")	
							
			// diff-party point estimate and 95% confidence interval for figure
			if `k'==2 {
				local mpvote_`r'_out = `coef_`r'_av'/`sd`r'raw'
				local mpvotel_`r'_out = (`coef_`r'_av' - invttail(e(df_r),0.025)*`se_`r'_av')/`sd`r'raw'
				local mpvoteu_`r'_out = (`coef_`r'_av' + invttail(e(df_r),0.025)*`se_`r'_av')/`sd`r'raw'
			}		
			
			// randomization inference p-values
			if `RI'==1 {
				local Nbeta_extreme = 0
				forv i=1/`runsRI' {
					areg `measure'`time' fracOther`Hetero'NeighborRI`i' if sampleSession`time', absorb(strataSessionPartyFE)
					local absCoef = abs(_b[fracOther`Hetero'NeighborRI`i'])
					local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`time'`r'_av')) // + 1 every time see more extreme beta
				}
				local pval_`time'`r'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
			}
			else {
				local pval_`time'`r'_RI = 999
			}
			
			// get stars for all pvals
			foreach y in av RI {
				local pstar_`time'`r'_`y' = "" // reset p-value
				if `pval_`time'`r'_`y''<0.10 & `pval_`time'`r'_`y''>=0.05 local pstar_`time'`r'_`y' = "*"
				if `pval_`time'`r'_`y''<0.05 & `pval_`time'`r'_`y''>=0.01 local pstar_`time'`r'_`y' = "**"
				if `pval_`time'`r'_`y''<0.01 local pstar_`time'`r'_`y' = "***"
			}	
			
			// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
			if `pval_`time'`r'_av'<0.001 local pval_`time'`r'_av = "<0.001" 	
		}
	}
	
	// Output Main Effects Table
	estout MP1 MP2 MP3 MP4 using "`tablefolder'//mpVoteContest`Hetero'A.tex", style(tex) replace ///
	keep(fracOther`Hetero'NeighborRI0) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(fracOther`Hetero'NeighborRI0 "Proportion Other-`Hetero' Neighbor")

	estout MPLead1 MPLead2 MPLead3 MPLead4 using "`tablefolder'//mpVoteContest`Hetero'B.tex", style(tex) replace ///
	keep(fracOther`Hetero'NeighborRI0) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(fracOther`Hetero'NeighborRI0 "Proportion Other-`Hetero' Neighbor")

	estout MPLag1 MPLag2 MPLag3 MPLag4 using "`tablefolder'//mpVoteContest`Hetero'C.tex", style(tex) replace ///
	keep(fracOther`Hetero'NeighborRI0) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(fracOther`Hetero'NeighborRI0 "Proportion Other-`Hetero' Neighbor")

	local tex "& \multicolumn{4}{c}{\textit{Panel A:} Contemporaneous Effect (t)} \vspace{0.2em} \\"
	local tex "`tex' \input{mpVoteContest`Hetero'A} \vspace{-1.2em} \\"
	local tex "`tex' & (`se_1_av') & (`se_2_av') & (`se_3_av') & (`se_4_av') \\"
	local tex "`tex' & [`pval_1_av']`pstar_1_av' & [`pval_2_av']`pstar_2_av' & [`pval_3_av']`pstar_3_av' & [`pval_4_av']`pstar_4_av' \\"
	local tex "`tex' & \{`pval_1_RI'\}`pstar_1_RI' & \{`pval_2_RI'\}`pstar_2_RI' & \{`pval_3_RI'\}`pstar_3_RI' & \{`pval_4_RI'\}`pstar_4_RI' \\ \\[-5pt]"	
	local tex "`tex' Observations & `obs1' & `obs2' & `obs3' & `obs4' \\ \hline"
	local tex "`tex' \vspace{-8pt} & & & & \\"
	local tex "`tex' & \multicolumn{4}{c}{\textit{Panel B:} One Year Later (t+1)} \vspace{0.2em} \\"  
	local tex "`tex' \input{mpVoteContest`Hetero'B} \vspace{-1.2em} \\" 
	local tex "`tex'  & (`se_Lead1_av') & (`se_Lead2_av') & (`se_Lead3_av') & (`se_Lead4_av') \\"
	local tex "`tex'  & [`pval_Lead1_av']`pstar_Lead1_av' & [`pval_Lead2_av']`pstar_Lead2_av' & [`pval_Lead3_av']`pstar_Lead3_av' & [`pval_Lead4_av']`pstar_Lead4_av' \\"
	local tex "`tex' & \{`pval_Lead1_RI'\}`pstar_Lead1_RI' & \{`pval_Lead2_RI'\}`pstar_Lead2_RI' & \{`pval_Lead3_RI'\}`pstar_Lead3_RI' & \{`pval_Lead4_RI'\}`pstar_Lead4_RI' \\ \\[-5pt]"		
	local tex "`tex' Observations & `obsLead1' & `obsLead2' & `obsLead3' & `obsLead4' \\ \hline" 
	local tex "`tex' \vspace{-8pt} & & & & \\"
	local tex "`tex' & \multicolumn{4}{c}{\textit{Panel C:} Previous Year (Placebo) (t-1)} \vspace{0.2em} \\"  
	local tex "`tex' \input{mpVoteContest`Hetero'C} \vspace{-1.2em} \\"
	local tex "`tex'  & (`se_Lag1_av') & (`se_Lag2_av') & (`se_Lag3_av') & (`se_Lag4_av') \\"
	local tex "`tex'  & [`pval_Lag1_av']`pstar_Lag1_av' & [`pval_Lag2_av']`pstar_Lag2_av' & [`pval_Lag3_av']`pstar_Lag3_av' & [`pval_Lag4_av']`pstar_Lag4_av' \\"
	local tex "`tex' & \{`pval_Lag1_RI'\}`pstar_Lag1_RI' & \{`pval_Lag2_RI'\}`pstar_Lag2_RI' & \{`pval_Lag3_RI'\}`pstar_Lag3_RI' & \{`pval_Lag4_RI'\}`pstar_Lag4_RI' \\ \\[-5pt]"			
	local tex "`tex' Observations & `obsLag1' & `obsLag2' & `obsLag3' & `obsLag4' \\ \hline" 
	local tex "`tex' Session $\times$ Party $\times$ Strata FE & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `mean3' & `mean4' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sd3' & `sd4' \\ \toprule" 
	local tex "`tex' \multicolumn{5}{p{16.8cm}}{\footnotesize \textit{Notes:} `eachPanelFour'" 
	local tex "`tex' `contested' `noncomplianceMP' `rebelWeakMP' `propothMP'"
	local tex "`tex' `mpSEp' `riCurly'"
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDefMP' `outcomeMeanA' `plab'}" 
	local tex "`tex' \\ \end{tabular} }"
	
	esttab MP1 MP2 MP3 MP4 using "`tablefolder'//mpVoteContest`Hetero'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("Below 50th Votes" "Below 25th Votes", pattern(1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Leader\\Non-Compliance\\(1)}" "\specialcell{Rebel\\Rate\\(2)}" ///
	"\specialcell{Leader\\Non-Compliance\\(3)}" "\specialcell{Rebel\\Rate\\(4)}")	

	timer off 10
	disp "DONE mpVoteContest"
	timer list 10

}
if `mpCosponOneTwo'==1 { // MP-session-level cosponsorship results (by intensity of contact)

	timer on 11
	use "`votingfolder'/MP_session_analysis", clear

	// Define sessions that we will use
	g sampleSession = (!specialSession) & (!shortSession) & !(session_id == 149)
	g sampleSessionLag = !inlist(session_id,115,119,120,124,125,129,130,134,135,137,138,142,143,146,147,148,149)
	g sampleSessionLead = !inlist(session_id,118,119,123,124,128,129,133,134,136,137,141,142,145,146,147,148,149)

	// Three panels: contemporary, lead, lag (placebo)
	forv k = 1/3 { 		// Time period?
		local time: word `k' of "Lag" "" "Lead"	
	
		forv r = 1/2 {	// Type of voting/cosponsorship measure?
			local measure: word `r' of "cospon_all" "ihs_cospon_all"		
			areg `measure'`time' halfOther`Hetero'NeighborRI0 allOther`Hetero'NeighborRI0 if sampleSession`time', absorb(strataSessionPartyFE) cluster(MP_id)
			est sto MP`time'`r'
			local obs`time'`r' = e(N)	
			
			// summary stats
			qui su `measure'`time' if e(sample)==1
			local mean`time'`r' = string(r(mean),"%9.2g")
			local sd`time'`r' = string(r(sd),"%9.2g")
			local sd`time'`r'raw = `r(sd)'	
			
			// prepare coefficient, standard error, p-value for export
			foreach x in half all {
				local coef_`time'`r'_`x' = string(_b[`x'Other`Hetero'NeighborRI0],"%9.2g")
				local se_`time'`r'_`x' = string(_se[`x'Other`Hetero'NeighborRI0],"%9.2g")
				local pval_`time'`r'_`x' = string(ttail(e(df_r),abs(`coef_`time'`r'_`x''/`se_`time'`r'_`x''))*2,"%9.2g")	
			}	
				
			// diff-party point estimate and 95% confidence interval for figure
			/*
			if `k'==2 {
				local mp_`r'_out = `coef_`r'_av'/`sd`r'raw'
				local mpl_`r'_out = (`coef_`r'_av' - invttail(e(df_r),0.025)*`se_`r'_av')/`sd`r'raw'
				local mpu_`r'_out = (`coef_`r'_av' + invttail(e(df_r),0.025)*`se_`r'_av')/`sd`r'raw'
			}
			*/
			
			// randomization inference p-values
			if `RI'==1 {
				local Nbeta_extreme_half = 0
				local Nbeta_extreme_all = 0
				forv i=1/`runsRI' {
					areg `measure'`time' halfOther`Hetero'NeighborRI`i' allOther`Hetero'NeighborRI`i' if sampleSession`time', absorb(strataSessionPartyFE)
					
					foreach x in half all {
						local absCoef_`x' = abs(_b[`x'Other`Hetero'NeighborRI`i'])
						local Nbeta_extreme_`x' = `Nbeta_extreme_`x'' + (`absCoef_`x'' >= abs(`coef_`time'`r'_`x'')) // + 1 every time see more extreme beta
					}
				}
				local pval_`time'`r'_RI_half = string(`Nbeta_extreme_half'/`runsRI',"%9.2g")
				local pval_`time'`r'_RI_all = string(`Nbeta_extreme_all'/`runsRI',"%9.2g")	
			}
			else {
				local pval_`time'`r'_RI_half = 999
				local pval_`time'`r'_RI_all = 999				
			}
			
			// get stars for all pvals
			foreach y in half all RI_half RI_all {
				local pstar_`time'`r'_`y' = "" // reset p-value
				if `pval_`time'`r'_`y''<0.10 & `pval_`time'`r'_`y''>=0.05 local pstar_`time'`r'_`y' = "*"
				if `pval_`time'`r'_`y''<0.05 & `pval_`time'`r'_`y''>=0.01 local pstar_`time'`r'_`y' = "**"
				if `pval_`time'`r'_`y''<0.01 local pstar_`time'`r'_`y' = "***"
			}
			
			// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
			foreach y in half all {
				if `pval_`time'`r'_`y''<0.001 local pval_`time'`r'_`y' = "<0.001" 
			}				
		}
	}
	
	// Output Heterogeneous Effects Table
	local tex "Proportion Other-`Hetero' Neighbor = 1/2 & `coef_1_half' & `coef_2_half' & `coef_Lead1_half' & `coef_Lead2_half' & `coef_Lag1_half' & `coef_Lag2_half' \\"
	local tex "`tex' & [`pval_1_half']`pstar_1_half' & [`pval_2_half']`pstar_2_half' & [`pval_Lead1_half']`pstar_Lead1_half' & [`pval_Lead2_half']`pstar_Lead2_half' & [`pval_Lag1_half']`pstar_Lag1_half' & [`pval_Lag2_half']`pstar_Lag2_half' \\"
	local tex "`tex' & \{`pval_1_RI_half'\}`pstar_1_RI_half' & \{`pval_2_RI_half'\}`pstar_2_RI_half' & \{`pval_Lead1_RI_half'\}`pstar_Lead1_RI_half' & \{`pval_Lead2_RI_half'\}`pstar_Lead2_RI_half' & \{`pval_Lag1_RI_half'\}`pstar_Lag1_RI_half' & \{`pval_Lag2_RI_half'\}`pstar_Lag2_RI_half' \\ \\[-5pt]"		
	
	local tex "`tex' Proportion Other-`Hetero' Neighbor = 1 & `coef_1_all' & `coef_2_all' & `coef_Lead1_all' & `coef_Lead2_all'  & `coef_Lag1_all' & `coef_Lag2_all' \\"
	local tex "`tex'  & [`pval_1_all']`pstar_1_all' & [`pval_2_all']`pstar_2_all' & [`pval_Lead1_all']`pstar_Lead1_all' & [`pval_Lead2_all']`pstar_Lead2_all' & [`pval_Lag1_all']`pstar_Lag1_all' & [`pval_Lag2_all']`pstar_Lag2_all' \\"
	local tex "`tex' & \{`pval_1_RI_all'\}`pstar_1_RI_all' & \{`pval_2_RI_all'\}`pstar_2_RI_all' & \{`pval_Lead1_RI_all'\}`pstar_Lead1_RI_all' & \{`pval_Lead2_RI_all'\}`pstar_Lead2_RI_all' & \{`pval_Lag1_RI_all'\}`pstar_Lag1_RI_all' & \{`pval_Lag2_RI_all'\}`pstar_Lag2_RI_all' \\ \\[-5pt] \hline"		
		
	local tex "`tex' Observations & `obs1' & `obs2' & `obsLead1' & `obsLead2' & `obsLag1' & `obsLag2' \\" 
	local tex "`tex' Session $\times$ Party $\times$ Strata FE & Y & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `meanLead1' & `meanLead2' & `meanLag1' & `meanLag2' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sdLead1' & `sdLead2' & `sdLag1' & `sdLag2' \\ \toprule" 
	
	local tex "`tex' \multicolumn{7}{p{16.8cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `rawnumbMP' `ihsAll'"
	local tex "`tex' `propothMP' `mpSquare' `riCurly'"
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDefMP' `plab'}"
	local tex "`tex' \\ \end{tabular} }"
	
	esttab MP1 MP2 MPLead1 MPLead2 MPLag1 MPLag2 using "`tablefolder'//mpCosponOneTwo`Hetero'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("\specialcell{Contemporaneous\\Effect (t)}" "\specialcell{One Year\\Later (t+1)}" ///
			"\specialcell{Previous Year\\(Placebo) (t-1)}", pattern(1 0 1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Number\\(1)}" "\specialcell{IHS\\(2)}" ///
	"\specialcell{Number\\(3)}" "\specialcell{IHS\\(4)}" ///	
	"\specialcell{Number\\(5)}" "\specialcell{IHS\\(6)}")		
	
	timer off 11
	disp "DONE mpCosponOneTwo"
	timer list 11
}
if `dyadVoteRobust'==1 { // Dyadic voting results (robustness)
	
	timer on 12

	forv r = 1/5 {	// Type of voting/cosponsorship measure?
		local measure: word `r' of "samevote4c" "voteSim4c" "samevote3c" "voteSim3c" "voteSim4cFlip"
			
			dis "`measure'"
			use `datamod', clear
			keep session_id samevote?c* samevotePresent samePresentAbsent votediff?c* voteSim?c* *RI* minMP maxMP ///
			pairStrata samecoalition sameparty ///
			samegender specialSession shortSession firstSession lastSession leadShortIncomplete	party_id party_id_d	///
			sessionXpairStrataXpartyPair
			
			/* Neighborhood effect */
			
			// residualize
			qui areg `measure' if !mi(`measure') & !mi(neighborRI0) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
			local numN = e(N)
			local numDummy = e(df_a) 
			predict yresid, residuals
			qui areg neighborRI0 if !mi(`measure') & !mi(neighborRI0) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
			local numN2 = e(N)
			predict xresid, residuals
			assert `numN' == `numN2'
			
			// run regression
			xi: regdyad2 yresid xresid if !mi(`measure') & !mi(neighborRI0) & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d), ///
				dyads(minMP maxMP)
			est sto M`r'
			local obs`r' = e(N)
			qui su `measure' if e(sample)==1
			local mean`r' = string(r(mean),"%9.2g")
			local sd`r' = string(r(sd),"%9.2g")
			local sd`r'raw = `r(sd)'
			local numReg = 1
			
			// prepare coefficient, standard error, p-value for export
			local coef_`r'_av = _b[xresid]
			local se_`r'_av = string(_se[xresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1)),"%9.2g")
			local t_df = `numN'-`numReg'-`numDummy'-1
			local pval_`r'_av = string(ttail(`t_df',abs(`coef_`r'_av'/`se_`r'_av'))*2,"%9.2g")					
			drop yresid xresid	
			
			// randomization inference p-values
			if `RI'==1 {
				local Nbeta_extreme = 0
				forv i=1/`runsRI' {
					areg `measure' neighborRI`i', absorb(sessionXpairStrataXpartyPair)
					local absCoef = abs(_b[neighborRI`i'])
					local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`r'_av')) // + 1 every time see more extreme beta
				}
				local pval_`r'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
			}
			else {
				local pval_`r'_RI = 999
			}				
			
			/* Hetero: party ingroup and outgroup effect */
			
			// residualize			
			qui areg `measure' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair)
			local numN = e(N)
			local numDummy = e(df_a) 
			predict yresid, residuals
			
			qui areg neighborRI0Xsame`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
			local numN2 = e(N)
			predict xinresid, residuals
			assert `numN' == `numN2'
			qui areg neighborRI0Xdiff`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
			predict xoutresid, residuals
			
			// run regression
			xi: regdyad2 yresid xinresid xoutresid if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') ///
				& !mi(pairStrata) & !mi(party_id) & !mi(party_id_d), dyads(minMP maxMP)
			est sto Het`r'
			local obsHet`r' = e(N)
			local numReg = 2
			matrix varMatrix = e(V)
			
			// prepare coefficient, standard error, p-value for export					
			local coef_`r'_in = string(_b[xinresid],"%9.2g")
			local coef_`r'_out = string(_b[xoutresid],"%9.2g")
			local se_`r'_in = _se[xinresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
			local se_`r'_out = _se[xoutresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
			local t_df = `numN'-`numReg'-`numDummy'-1
			local pval_`r'_in =  string(ttail(`t_df',abs(`coef_`r'_in'/`se_`r'_in'))*2,"%9.2g")
			local pval_`r'_out =  string(ttail(`t_df',abs(`coef_`r'_out'/`se_`r'_out'))*2,"%9.2g")
			
			// pval calculation for the difference
			local diff = `coef_`r'_in' - `coef_`r'_out'
			local se_`r'_diff = sqrt(varMatrix[1,1]+varMatrix[2,2]-2*varMatrix[2,1])
			local pval_`r'_diff =  string(ttail(`t_df',abs(`diff'/`se_`r'_diff'))*2,"%9.2g")
			drop yresid xinresid xoutresid
			
			// diff-party point estimate and 95% confidence interval for figure
			/*
			local core_`r'_out = `coef_`r'_out'/`sd`r'raw'
			local corel_`r'_out = (`coef_`r'_out' - invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
			local coreu_`r'_out = (`coef_`r'_out' + invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
			*/
			
			// randomization inference p-values
			if `RI'==1 {
				local Nbeta_extreme_in = 0
				local Nbeta_extreme_out = 0
				local Nbeta_extreme_diff = 0
				forv i=1/`runsRI' {
					areg `measure' neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero', absorb(sessionXpairStrataXpartyPair)
					local absCoefIn = abs(_b[neighborRI`i'Xsame`hetero'])
					local absCoefOut = abs(_b[neighborRI`i'Xdiff`hetero'])
					local Nbeta_extreme_in = `Nbeta_extreme_in' + (`absCoefIn' >= abs(`coef_`r'_in')) // + 1 every time see more extreme beta
					local Nbeta_extreme_out = `Nbeta_extreme_out' + (`absCoefOut' >= abs(`coef_`r'_out')) 
				
					// for difference between in vs out p-value we need separate regressions, using "modified" outcome
					g `measure'_m = `measure'
					replace `measure'_m = `measure'_m + `coef_`r'_av' if neighborRI`i' == 1 & neighborRI0 == 0
					replace `measure'_m = `measure'_m - `coef_`r'_av' if neighborRI`i' == 0 & neighborRI0 == 1	
					
					// run regression with modified outcome
					areg `measure'_m neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero', absorb(sessionXpairStrataXpartyPair)
					local absCoefDiff = abs(_b[neighborRI`i'Xsame`hetero']-_b[neighborRI`i'Xdiff`hetero'])
					local Nbeta_extreme_diff = `Nbeta_extreme_diff' + (`absCoefDiff' >= abs(`diff')) 
					drop `measure'_m
				}
				local pval_`r'_RIin = string(`Nbeta_extreme_in'/`runsRI',"%9.2g")
				local pval_`r'_RIout = string(`Nbeta_extreme_out'/`runsRI',"%9.2g")	
				local pval_`r'_RIdiff = string(`Nbeta_extreme_diff'/`runsRI',"%9.2g")	
			}
			else {
				local pval_`r'_RIin = 999
				local pval_`r'_RIout = 999	
				local pval_`r'_RIdiff = 999										
			}					
			
			// get stars for all pvals
			foreach y in av in out diff RI RIin RIout RIdiff {
				local pstar_`r'_`y' = "" // reset p-value
				if `pval_`r'_`y''<0.10 & `pval_`r'_`y''>=0.05 local pstar_`r'_`y' = "*"
				if `pval_`r'_`y''<0.05 & `pval_`r'_`y''>=0.01 local pstar_`r'_`y' = "**"
				if `pval_`r'_`y''<0.01 local pstar_`r'_`y' = "***"
			}
	
			// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
			foreach y in av in out diff {
				if `pval_`r'_`y''<0.001 local pval_`r'_`y' = "<0.001" 
			}						
	}
		
	// Output Main Effects Table (appendix?)
	estout M1 M2 M3 M4 M5 using "`tablefolder'//pairVoteRobustA.tex", style(tex) replace ///
	keep(xresid) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(xresid "Neighbor")

	local tex "\input{pairVoteRobustA} \vspace{-1.2em} \\"
	local tex "`tex' & (`se_1_av') & (`se_2_av') & (`se_3_av') & (`se_4_av') & (`se_5_av') \\"
	local tex "`tex' & [`pval_1_av']`pstar_1_av' & [`pval_2_av']`pstar_2_av' & [`pval_3_av']`pstar_3_av' & [`pval_4_av']`pstar_4_av' & [`pval_5_av']`pstar_5_av' \\"
	local tex "`tex' & \{`pval_1_RI'\}`pstar_1_RI' & \{`pval_2_RI'\}`pstar_2_RI' &  \{`pval_3_RI'\}`pstar_3_RI' & \{`pval_4_RI'\}`pstar_4_RI' & \{`pval_5_RI'\}`pstar_5_RI' \\ \\[-5pt] \hline"	
	
	local tex "`tex' Observations & `obs1' & `obs2' & `obs3' & `obs4' & `obs5' \\"
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `mean3' & `mean4' & `mean5' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sd3' & `sd4' & `sd5' \\ \toprule" 
	
	local tex "`tex' \multicolumn{6}{p{18.5cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `comp3' `sim3' `simRec'"
	local tex "`tex' `neighbor'"
	local tex "`tex' `dyadSEp' `riCurly'" 
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `plab'}"	
	local tex "`tex' \\ \end{tabular} }"
	
	esttab M1 M2 M3 M4 M5 using "`tablefolder'//pairVoteRobust.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("Contemporaneous Effect (t)", pattern(1 0 0 0 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{\\Compliance\\(1)}" "\specialcell{\\Similarity\\(2)}" ///
	"\specialcell{Compliance\\3-Cat\\(3)}" "\specialcell{Similarity\\3-Cat\\(4)}" ///
	"\specialcell{Similarity\\Recode\\(5)}")	
	
	// Output Heterogeneous Effects Table
	local tex "Neighbor $\times$ Different `Hetero' & `coef_1_out' & `coef_2_out' & `coef_3_out' & `coef_4_out' & `coef_5_out' \\"
	local tex "`tex' `bipart' & [`pval_1_out']`pstar_1_out' & [`pval_2_out']`pstar_2_out' & [`pval_3_out']`pstar_3_out' & [`pval_4_out']`pstar_4_out' & [`pval_5_out']`pstar_5_out' \\"
	local tex "`tex' & \{`pval_1_RIout'\}`pstar_1_RIout' & \{`pval_2_RIout'\}`pstar_2_RIout' & \{`pval_3_RIout'\}`pstar_3_RIout' & \{`pval_4_RIout'\}`pstar_4_RIout' & \{`pval_5_RIout'\}`pstar_5_RIout' \\ \\[-5pt]"		
	
	local tex "`tex' Neighbor $\times$ Same `Hetero' & `coef_1_in' & `coef_2_in' & `coef_3_in' & `coef_4_in'  & `coef_5_in' \\"
	local tex "`tex'  & [`pval_1_in']`pstar_1_in' & [`pval_2_in']`pstar_2_in' & [`pval_3_in']`pstar_3_in' & [`pval_4_in']`pstar_4_in' & [`pval_5_in']`pstar_5_in' \\"
	local tex "`tex' & \{`pval_1_RIin'\}`pstar_1_RIin' & \{`pval_2_RIin'\}`pstar_2_RIin' & \{`pval_3_RIin'\}`pstar_3_RIin' & \{`pval_4_RIin'\}`pstar_4_RIin' & \{`pval_5_RIin'\}`pstar_5_RIin' \\ \\[-5pt]"		
				
	local tex "`tex' Same = Different & [`pval_1_diff']`pstar_1_diff' & [`pval_2_diff']`pstar_2_diff' & [`pval_3_diff']`pstar_3_diff' & [`pval_4_diff']`pstar_4_diff' & [`pval_5_diff']`pstar_5_diff' \\"
	local tex "`tex' & \{`pval_1_RIdiff'\}`pstar_1_RIdiff' & \{`pval_2_RIdiff'\}`pstar_2_RIdiff' & \{`pval_3_RIdiff'\}`pstar_3_RIdiff' & \{`pval_4_RIdiff'\}`pstar_4_RIdiff' & \{`pval_5_RIdiff'\}`pstar_5_RIdiff' \\ \\[-5pt] \hline"				
	
	local tex "`tex' Observations & `obsHet1' & `obsHet2' & `obsHet3' & `obsHet4' & `obsHet5' \\" 
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `mean3' & `mean4' & `mean5' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sd3' & `sd4' & `sd5' \\ \toprule" 
	
	local tex "`tex' \multicolumn{6}{p{17cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' The first two columns replicate the core results of Table 1."
	local tex "`tex' `comp3' `sim3' `simRec'"
	local tex "`tex' `neighbor' `samehet'"
	local tex "`tex' `dyadSquare' `riCurly'"
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `plab'}"
	local tex "`tex' \\ \end{tabular} }"
	
	esttab Het1 Het2 Het3 Het4 Het5 using "`tablefolder'//pairVoteRobust`Hetero'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("Contemporaneous Effect (t)", pattern(1 0 0 0 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{\\Compliance\\(1)}" "\specialcell{\\Similarity\\(2)}" ///
	"\specialcell{Compliance\\3-Cat\\(3)}" "\specialcell{Similarity\\3-Cat\\(4)}" ///
	"\specialcell{Similarity\\Recode\\(5)}")		
	
	timer off 12
	disp "DONE dyadVoteRobust"
	timer list 12
}
if `dyadVoteContestRobust'==1 { // Dyadic voting results (contested, robustness)
	
	timer on 13

	forv r = 1/5 {	// Type of voting/cosponsorship measure?
		local measure: word `r' of "samevote4c_VS" "voteSim4c_VS" "samevote3c_VS" "voteSim3c_VS" "voteSim4cFlip_VS"
		
			dis "`measure'"
			use `datamod', clear
			keep session_id samevote?c* samevotePresent_VS samePresentAbsent_VS votediff?c* voteSim?c* *RI* minMP maxMP ///
			pairStrata samecoalition sameparty ///
			samegender specialSession shortSession firstSession lastSession leadShortIncomplete	party_id party_id_d	///
			sessionXpairStrataXpartyPair
			
			/* Neighborhood effect */
			
			// residualize
			qui areg `measure' if !mi(`measure') & !mi(neighborRI0) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
			local numN = e(N)
			local numDummy = e(df_a) 
			predict yresid, residuals
			qui areg neighborRI0 if !mi(`measure') & !mi(neighborRI0) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
			local numN2 = e(N)
			predict xresid, residuals
			assert `numN' == `numN2'
			
			// run regression
			xi: regdyad2 yresid xresid if !mi(`measure') & !mi(neighborRI0) & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d), ///
				dyads(minMP maxMP)
			est sto M`r'
			local obs`r' = e(N)
			qui su `measure' if e(sample)==1
			local mean`r' = string(r(mean),"%9.2g")
			local sd`r' = string(r(sd),"%9.2g")
			local sd`r'raw = `r(sd)'
			local numReg = 1
			
			// prepare coefficient, standard error, p-value for export
			local coef_`r'_av = _b[xresid]
			local se_`r'_av = string(_se[xresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1)),"%9.2g")
			local t_df = `numN'-`numReg'-`numDummy'-1
			local pval_`r'_av = string(ttail(`t_df',abs(`coef_`r'_av'/`se_`r'_av'))*2,"%9.2g")					
			drop yresid xresid	
			
			// randomization inference p-values
			if `RI'==1 {
				local Nbeta_extreme = 0
				forv i=1/`runsRI' {
					areg `measure' neighborRI`i', absorb(sessionXpairStrataXpartyPair)
					local absCoef = abs(_b[neighborRI`i'])
					local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`r'_av')) // + 1 every time see more extreme beta
				}
				local pval_`r'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
			}
			else {
				local pval_`r'_RI = 999
			}				
			
			/* Hetero: party ingroup and outgroup effect */
			
			// residualize			
			qui areg `measure' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair)
			local numN = e(N)
			local numDummy = e(df_a) 
			predict yresid, residuals
			
			qui areg neighborRI0Xsame`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
			local numN2 = e(N)
			predict xinresid, residuals
			assert `numN' == `numN2'
			qui areg neighborRI0Xdiff`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
			predict xoutresid, residuals
			
			// run regression
			xi: regdyad2 yresid xinresid xoutresid if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') ///
				& !mi(pairStrata) & !mi(party_id) & !mi(party_id_d), dyads(minMP maxMP)
			est sto Het`r'
			local obsHet`r' = e(N)
			local numReg = 2
			matrix varMatrix = e(V)
			
			// prepare coefficient, standard error, p-value for export					
			local coef_`r'_in = string(_b[xinresid],"%9.2g")
			local coef_`r'_out = string(_b[xoutresid],"%9.2g")
			local se_`r'_in = _se[xinresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
			local se_`r'_out = _se[xoutresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
			local t_df = `numN'-`numReg'-`numDummy'-1
			local pval_`r'_in =  string(ttail(`t_df',abs(`coef_`r'_in'/`se_`r'_in'))*2,"%9.2g")
			local pval_`r'_out =  string(ttail(`t_df',abs(`coef_`r'_out'/`se_`r'_out'))*2,"%9.2g")
			
			// pval calculation for the difference
			local diff = `coef_`r'_in' - `coef_`r'_out'
			local se_`r'_diff = sqrt(varMatrix[1,1]+varMatrix[2,2]-2*varMatrix[2,1])
			local pval_`r'_diff =  string(ttail(`t_df',abs(`diff'/`se_`r'_diff'))*2,"%9.2g")
			drop yresid xinresid xoutresid
			
			// diff-party point estimate and 95% confidence interval for figure
			/*
			local core_`r'_out = `coef_`r'_out'/`sd`r'raw'
			local corel_`r'_out = (`coef_`r'_out' - invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
			local coreu_`r'_out = (`coef_`r'_out' + invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
			*/
			
			// randomization inference p-values
			if `RI'==1 {
				local Nbeta_extreme_in = 0
				local Nbeta_extreme_out = 0
				local Nbeta_extreme_diff = 0
				forv i=1/`runsRI' {
					areg `measure' neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero', absorb(sessionXpairStrataXpartyPair)
					local absCoefIn = abs(_b[neighborRI`i'Xsame`hetero'])
					local absCoefOut = abs(_b[neighborRI`i'Xdiff`hetero'])
					local Nbeta_extreme_in = `Nbeta_extreme_in' + (`absCoefIn' >= abs(`coef_`r'_in')) // + 1 every time see more extreme beta
					local Nbeta_extreme_out = `Nbeta_extreme_out' + (`absCoefOut' >= abs(`coef_`r'_out')) 
				
					// for difference between in vs out p-value we need separate regressions, using "modified" outcome
					g `measure'_m = `measure'
					replace `measure'_m = `measure'_m + `coef_`r'_av' if neighborRI`i' == 1 & neighborRI0 == 0
					replace `measure'_m = `measure'_m - `coef_`r'_av' if neighborRI`i' == 0 & neighborRI0 == 1	
					
					// run regression with modified outcome
					areg `measure'_m neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero', absorb(sessionXpairStrataXpartyPair)
					local absCoefDiff = abs(_b[neighborRI`i'Xsame`hetero']-_b[neighborRI`i'Xdiff`hetero'])
					local Nbeta_extreme_diff = `Nbeta_extreme_diff' + (`absCoefDiff' >= abs(`diff')) 
					drop `measure'_m
				}
				local pval_`r'_RIin = string(`Nbeta_extreme_in'/`runsRI',"%9.2g")
				local pval_`r'_RIout = string(`Nbeta_extreme_out'/`runsRI',"%9.2g")	
				local pval_`r'_RIdiff = string(`Nbeta_extreme_diff'/`runsRI',"%9.2g")	
			}
			else {
				local pval_`r'_RIin = 999
				local pval_`r'_RIout = 999	
				local pval_`r'_RIdiff = 999										
			}					
			
			// get stars for all pvals
			foreach y in av in out diff RI RIin RIout RIdiff {
				local pstar_`r'_`y' = "" // reset p-value
				if `pval_`r'_`y''<0.10 & `pval_`r'_`y''>=0.05 local pstar_`r'_`y' = "*"
				if `pval_`r'_`y''<0.05 & `pval_`r'_`y''>=0.01 local pstar_`r'_`y' = "**"
				if `pval_`r'_`y''<0.01 local pstar_`r'_`y' = "***"
			}
			
			// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
			foreach y in av in out diff {
				if `pval_`r'_`y''<0.001 local pval_`r'_`y' = "<0.001" 
			}								
	}
		
	// Output Main Effects Table (appendix?)
	estout M1 M2 M3 M4 M5 using "`tablefolder'//pairVoteContestRobustA.tex", style(tex) replace ///
	keep(xresid) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(xresid "Neighbor")

	local tex "\input{pairVoteContestRobustA} \vspace{-1.2em} \\"
	local tex "`tex' & (`se_1_av') & (`se_2_av') & (`se_3_av') & (`se_4_av') & (`se_5_av') \\"
	local tex "`tex' & [`pval_1_av']`pstar_1_av' & [`pval_2_av']`pstar_2_av' & [`pval_3_av']`pstar_3_av' & [`pval_4_av']`pstar_4_av' & [`pval_5_av']`pstar_5_av' \\"
	local tex "`tex' & \{`pval_1_RI'\}`pstar_1_RI' & \{`pval_2_RI'\}`pstar_2_RI' &  \{`pval_3_RI'\}`pstar_3_RI' & \{`pval_4_RI'\}`pstar_4_RI' & \{`pval_5_RI'\}`pstar_5_RI' \\ \\[-5pt] \hline"	
	
	local tex "`tex' Observations & `obs1' & `obs2' & `obs3' & `obs4' & `obs5' \\"
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `mean3' & `mean4' & `mean5' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sd3' & `sd4' & `sd5' \\ \toprule" 
	
	local tex "`tex' \multicolumn{6}{p{18.5cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `comp3' `sim3' `simRec'"
	local tex "`tex' `neighbor'"
	local tex "`tex' `dyadSEp' `riCurly'" 
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `plab'}"	
	local tex "`tex' \\ \end{tabular} }"
	
	esttab M1 M2 M3 M4 M5 using "`tablefolder'//pairVoteContestRobust.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("Contemporaneous Effect (t), Below 50th", pattern(1 0 0 0 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{\\Compliance\\(1)}" "\specialcell{\\Similarity\\(2)}" ///
	"\specialcell{Compliance\\3-Cat\\(3)}" "\specialcell{Similarity\\3-Cat\\(4)}" ///
	"\specialcell{Similarity\\Recode\\(5)}")	
	
	// Output Heterogeneous Effects Table
	local tex "Neighbor $\times$ Different `Hetero' & `coef_1_out' & `coef_2_out' & `coef_3_out' & `coef_4_out' & `coef_5_out' \\"
	local tex "`tex' `bipart' & [`pval_1_out']`pstar_1_out' & [`pval_2_out']`pstar_2_out' & [`pval_3_out']`pstar_3_out' & [`pval_4_out']`pstar_4_out' & [`pval_5_out']`pstar_5_out' \\"
	local tex "`tex' & \{`pval_1_RIout'\}`pstar_1_RIout' & \{`pval_2_RIout'\}`pstar_2_RIout' & \{`pval_3_RIout'\}`pstar_3_RIout' & \{`pval_4_RIout'\}`pstar_4_RIout' & \{`pval_5_RIout'\}`pstar_5_RIout' \\ \\[-5pt]"		
	
	local tex "`tex' Neighbor $\times$ Same `Hetero' & `coef_1_in' & `coef_2_in' & `coef_3_in' & `coef_4_in'  & `coef_5_in' \\"
	local tex "`tex'  & [`pval_1_in']`pstar_1_in' & [`pval_2_in']`pstar_2_in' & [`pval_3_in']`pstar_3_in' & [`pval_4_in']`pstar_4_in' & [`pval_5_in']`pstar_5_in' \\"
	local tex "`tex' & \{`pval_1_RIin'\}`pstar_1_RIin' & \{`pval_2_RIin'\}`pstar_2_RIin' & \{`pval_3_RIin'\}`pstar_3_RIin' & \{`pval_4_RIin'\}`pstar_4_RIin' & \{`pval_5_RIin'\}`pstar_5_RIin' \\ \\[-5pt]"		
				
	local tex "`tex' Same = Different & [`pval_1_diff']`pstar_1_diff' & [`pval_2_diff']`pstar_2_diff' & [`pval_3_diff']`pstar_3_diff' & [`pval_4_diff']`pstar_4_diff' & [`pval_5_diff']`pstar_5_diff' \\"
	local tex "`tex' & \{`pval_1_RIdiff'\}`pstar_1_RIdiff' & \{`pval_2_RIdiff'\}`pstar_2_RIdiff' & \{`pval_3_RIdiff'\}`pstar_3_RIdiff' & \{`pval_4_RIdiff'\}`pstar_4_RIdiff' & \{`pval_5_RIdiff'\}`pstar_5_RIdiff' \\ \\[-5pt] \hline"				
	
	local tex "`tex' Observations & `obsHet1' & `obsHet2' & `obsHet3' & `obsHet4' & `obsHet5' \\" 
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `mean3' & `mean4' & `mean5' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sd3' & `sd4' & `sd5' \\ \toprule" 
	
	local tex "`tex' \multicolumn{6}{p{17cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' The first two columns replicate results from Table 2."
	local tex "`tex' `comp3' `sim3' `simRec'"
	local tex "`tex' `neighbor' `samehet'"
	local tex "`tex' `dyadSquare' `riCurly'"
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `plab'}"
	local tex "`tex' \\ \end{tabular} }"
	
	esttab Het1 Het2 Het3 Het4 Het5 using "`tablefolder'//pairVoteContestRobust`Hetero'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("Contemporaneous Effect (t), Below 50th", pattern(1 0 0 0 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{\\Compliance\\(1)}" "\specialcell{\\Similarity\\(2)}" ///
	"\specialcell{Compliance\\3-Cat\\(3)}" "\specialcell{Similarity\\3-Cat\\(4)}" ///
	"\specialcell{Similarity\\Recode\\(5)}")			

	timer off 13
	disp "DONE dyadVoteContestRobust"
	timer list 13
}
if `dyadBalance'==1 { // Dyadic balance checks

	timer on 14

	local r=1
	
	// read in and modify data
	use "`votingfolder'/dyads_voting", clear
	merge 1:1 session_id MP_id MP_id_d using "`cosponfolder'/dyads_with_cospon", assert(3) nogen
	if `dropspeaker'==1 {
		drop if speaker_in_pair==1
	}
	if `dropspecial'==1 {
		drop if specialSession==1
	}
	if `dropshort'==1 {
		drop if shortSession==1
	}	
	keep if session_id >= `fr' & session_id <= `to'
	
	keep session_id samevote?c* votediff?c* voteSim?c* *RI* minMP maxMP pairStrata samecoalition sameparty ///
	samegender specialSession shortSession firstSession lastSession leadShortIncomplete	party_id party_id_d	///
	sessionXpairStrataXpartyPair ageDiff sameconstituency constOrderDiff sameeverministerPast ///
	sameevercommitteechairLast sessExperDiff wagesDiff expensesDiff		
	
	// gen lagged neighbor
	sort minMP maxMP session_id, stable
	by minMP maxMP: g neighborRI0Lag = neighborRI0[_n-1]
	replace neighborRI0Lag=. if inlist(session_id,115,119,120,124,125,129,130,134,135,137,138,142,143,146,147,148,149)	
	
	// initialise tex rows
	local seText = ""
	local pText = ""
	local pRIText = ""
	local obsText = ""
	local YText = ""
	local meanText = ""
	local coefOutText = ""
	local pOutText = ""
	local pRIOutText = ""
	local coefInText = ""
	local pInText = ""
	local pRIInText = ""
	local pDiffText = ""
	local pRIDiffText = ""
	local obsHetText = ""
	
	// loop over predetermined vars
	foreach measure of varlist neighborRI0Lag samegender sameeverministerPast ///
				sameevercommitteechairLast sameconstituency ///
				ageDiff sessExperDiff wagesDiff expensesDiff {
			
		dis "`measure'"
		
		/* Neighborhood effect */
		
		// residualize
		qui areg `measure' if !mi(`measure') & !mi(neighborRI0) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
		local numN = e(N)
		local numDummy = e(df_a) 
		predict yresid, residuals
		qui areg neighborRI0 if !mi(`measure') & !mi(neighborRI0) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
		local numN2 = e(N)
		predict xresid, residuals
		assert `numN' == `numN2'
		
		// run regression
		xi: regdyad2 yresid xresid if !mi(`measure') & !mi(neighborRI0) & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d), ///
			dyads(minMP maxMP)
		est sto M`r'
		local obs`r' = e(N)
		qui su `measure' if e(sample)==1
		local mean`r' = string(r(mean),"%9.2g")
		local numReg = 1
		
		// prepare coefficient, standard error, p-value for export
		local coef_`r'_av = _b[xresid]
		local se_`r'_av = string(_se[xresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1)),"%9.2g")
		local t_df = `numN'-`numReg'-`numDummy'-1
		local pval_`r'_av = string(ttail(`t_df',abs(`coef_`r'_av'/`se_`r'_av'))*2,"%9.2g")					
		drop yresid xresid	
				
		// randomization inference p-values
		if `RI'==1 {
			local Nbeta_extreme = 0
			forv i=1/`runsRI' {
				areg `measure' neighborRI`i', absorb(sessionXpairStrataXpartyPair)
				local absCoef = abs(_b[neighborRI`i'])
				local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`r'_av')) // + 1 every time see more extreme beta
			}
			local pval_`r'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
		}
		else {
			local pval_`r'_RI = 999
		}
		
		// add to tex rows
		local seText = "`seText' & (`se_`r'_av')"
		local obsText = "`obsText' & `obs`r''"
		local YText = "`YText' & Y"
		local meanText = "`meanText' & `mean`r''"		

		
		/* Hetero: party ingroup and outgroup effect */
		
		// residualize			
		qui areg `measure' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair)
		local numN = e(N)
		local numDummy = e(df_a) 
		predict yresid, residuals
		
		qui areg neighborRI0Xsame`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
		local numN2 = e(N)
		predict xinresid, residuals
		assert `numN' == `numN2'
		qui areg neighborRI0Xdiff`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
		predict xoutresid, residuals
		
		// run regression
		xi: regdyad2 yresid xinresid xoutresid if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') ///
			& !mi(pairStrata) & !mi(party_id) & !mi(party_id_d), dyads(minMP maxMP)
		est sto Het`r'
		local obsHet`r' = e(N)
		local numReg = 2
		matrix varMatrix = e(V)
		
		// prepare coefficient, standard error, p-value for export					
		local coef_`r'_in = string(_b[xinresid],"%9.2g")
		local coef_`r'_out = string(_b[xoutresid],"%9.2g")
		local se_`r'_in = _se[xinresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
		local se_`r'_out = _se[xoutresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
		local t_df = `numN'-`numReg'-`numDummy'-1
		local pval_`r'_in =  string(ttail(`t_df',abs(`coef_`r'_in'/`se_`r'_in'))*2,"%9.2g")
		local pval_`r'_out =  string(ttail(`t_df',abs(`coef_`r'_out'/`se_`r'_out'))*2,"%9.2g")
		
		// pval calculation for the difference
		local diff = `coef_`r'_in' - `coef_`r'_out'
		local se_`r'_diff = sqrt(varMatrix[1,1]+varMatrix[2,2]-2*varMatrix[2,1])
		local pval_`r'_diff =  string(ttail(`t_df',abs(`diff'/`se_`r'_diff'))*2,"%9.2g")
		drop yresid xinresid xoutresid
		
		// randomization inference p-values
		if `RI'==1 {
			local Nbeta_extreme_in = 0
			local Nbeta_extreme_out = 0
			local Nbeta_extreme_diff = 0
			forv i=1/`runsRI' {
				areg `measure' neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero', absorb(sessionXpairStrataXpartyPair)
				local absCoefIn = abs(_b[neighborRI`i'Xsame`hetero'])
				local absCoefOut = abs(_b[neighborRI`i'Xdiff`hetero'])
				local Nbeta_extreme_in = `Nbeta_extreme_in' + (`absCoefIn' >= abs(`coef_`r'_in')) // + 1 every time see more extreme beta
				local Nbeta_extreme_out = `Nbeta_extreme_out' + (`absCoefOut' >= abs(`coef_`r'_out')) 
			
				// for difference between in vs out p-value we need separate regressions, using "modified" outcome
				g `measure'_m = `measure'
				replace `measure'_m = `measure'_m + `coef_`r'_av' if neighborRI`i' == 1 & neighborRI0 == 0
				replace `measure'_m = `measure'_m - `coef_`r'_av' if neighborRI`i' == 0 & neighborRI0 == 1	
				
				// run regression with modified outcome
				areg `measure'_m neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero', absorb(sessionXpairStrataXpartyPair)
				local absCoefDiff = abs(_b[neighborRI`i'Xsame`hetero']-_b[neighborRI`i'Xdiff`hetero'])
				local Nbeta_extreme_diff = `Nbeta_extreme_diff' + (`absCoefDiff' >= abs(`diff')) 
				drop `measure'_m
			}
			local pval_`r'_RIin = string(`Nbeta_extreme_in'/`runsRI',"%9.2g")
			local pval_`r'_RIout = string(`Nbeta_extreme_out'/`runsRI',"%9.2g")	
			local pval_`r'_RIdiff = string(`Nbeta_extreme_diff'/`runsRI',"%9.2g")	
		}
		else {
			local pval_`r'_RIin = 999
			local pval_`r'_RIout = 999	
			local pval_`r'_RIdiff = 999										
		}					
		
		// get stars for all pvals
		foreach y in av in out diff RI RIin RIout RIdiff {
			local pstar_`r'_`y' = "" // reset p-value
			if `pval_`r'_`y''<0.10 & `pval_`r'_`y''>=0.05 local pstar_`r'_`y' = "*"
			if `pval_`r'_`y''<0.05 & `pval_`r'_`y''>=0.01 local pstar_`r'_`y' = "**"
			if `pval_`r'_`y''<0.01 local pstar_`r'_`y' = "***"
		}
		
		// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
		foreach y in av in out diff {
			if `pval_`r'_`y''<0.001 local pval_`r'_`y' = "<0.001" 
		}			
		
		// add to tex rows
		local coefOutText = "`coefOutText' & `coef_`r'_out'"
		local pOutText = "`pOutText' & [`pval_`r'_out']`pstar_`r'_out'"
		local pRIOutText = "`pRIOutText' & \{`pval_`r'_RIout'\}`pstar_`r'_RIout'"
		local coefInText = "`coefInText' & `coef_`r'_in'"
		local pInText = "`pInText' & [`pval_`r'_in']`pstar_`r'_in'"
		local pRIInText = "`pRIInText' & \{`pval_`r'_RIin'\}`pstar_`r'_RIin'"
		local pDiffText = "`pDiffText' & [`pval_`r'_diff']`pstar_`r'_diff'"
		local pRIDiffText = "`pRIDiffText' & \{`pval_`r'_RIdiff'\}`pstar_`r'_RIdiff'"
		local obsHetText = "`obsHetText' & `obsHet`r''"
		local pText = "`pText' & [`pval_`r'_av']`pstar_`r'_av'"
		local pRIText = "`pRIText' & \{`pval_`r'_RI'\}`pstar_`r'_RI'"	
					
	local r=`r'+1
	}
		
	// Output Main Effects Table (appendix?)
	estout M1 M2 M3 M4 M5 M6 M7 M8 M9 using "`tablefolder'//pairBalanceA.tex", style(tex) replace ///
	keep(xresid) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(xresid "Neighbor")

	local tex "\input{pairBalanceA} \vspace{-1.2em} \\"
	local tex "`tex' `seText' \\"
	local tex "`tex' `pText' \\"
	local tex "`tex' `pRIText' \\ \\[-5pt] \hline"	
	
	local tex "`tex' Observations `obsText' \\"
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE `YText' \\"
	local tex "`tex' Outcome Mean `meanText' \\ \toprule"  
	
	local tex "`tex' \multicolumn{10}{p{22.6cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' The outcome in column (1) is a dummy variable equal to one if the two MPs"
	local tex "`tex' in a pair were seating neighbors in the previous session (only for non-short/special"
	local tex "`tex' sessions in same parliamentary term)."	
	local tex "`tex' Each outcome in columns (2) to (5) is a dummy variable equal to one"
	local tex "`tex' if the two MPs in a pair share the same value for the following variables:"
	local tex "`tex' (2) dummy variable equal to one if MP is male,"
	local tex "`tex' (3) dummy variable equal to one if ever held a ministerial position prior to this session,"
	local tex "`tex' (4) dummy variable equal to one if chaired a committee at any point during the previous session,"
	local tex "`tex' and (5) constituency."
	local tex "`tex' The outcomes in columns (6) to (9) are the absolute difference between the two MPs"
	local tex "`tex' in a pair for the following variables:"
	local tex "`tex' (6) age in years as of the start of the current session,"
	local tex "`tex' (7) number of sessions since first session as Althingi member,"
	local tex "`tex' (8) wages received in Althingi during the calendar year prior to the current session,"
	local tex "`tex' and (9) expenses claimed in Althingi during the calendar year prior to the current session."
	local tex "`tex' The waves and expenses data are only available from session 136 (2008/9) onwards."	
	local tex "`tex' `neighbor'"
	local tex "`tex' `dyadSEp' `riCurly'" 
	local tex "`tex' `specShortDrop'" 
	local tex "`tex' `strataDef' `plab'}"	
	local tex "`tex' \\ \end{tabular} }"
	
	esttab M1 M2 M3 M4 M5 M6 M7 M8 M9 using "`tablefolder'//pairBalance.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("" "Same..." "Difference in...", pattern(0 1 0 0 0 1 0 0 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Neighbor\\(t-1)\\(1)}" ///
	"\specialcell{\\Gender\\(2)}" ///
	"\specialcell{Ever\\Minister\\(3)}" ///
	"\specialcell{Committee\\(t-1)\\(4)}" ///
	"\specialcell{\\Constit.\\(5)}" ///	
	"\specialcell{\\Age\\(6)}" ///
	"\specialcell{Sessions\\Experience\\(7)}" ///
	"\specialcell{Wages\\(t-1)\\(8)}" ///
	"\specialcell{Expenses\\(t-1)\\(9)}")	
	
	
	// Output Heterogeneous Effects Table
	local tex "Neighbor $\times$ Different `Hetero' `coefOutText' \\"
	local tex "`tex' `bipart' `pOutText' \\"
	local tex "`tex' `pRIOutText' \\ \\[-5pt]"		
	
	local tex "`tex' Neighbor $\times$ Same `Hetero' `coefInText' \\"
	local tex "`tex' `pInText' \\"
	local tex "`tex' `pRIInText' \\ \\[-5pt]"		
		
	local tex "`tex' Same = Different `pDiffText' \\"
	local tex "`tex' `pRIDiffText' \\ \\[-5pt] \hline"				
	
	local tex "`tex' Observations `obsHetText' \\" 
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE `YText' \\"
	local tex "`tex' Outcome Mean `meanText' \\ \toprule"  
	
	local tex "`tex' \multicolumn{10}{p{23cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' The outcome in column (1) is a dummy variable equal to one if the two MPs"
	local tex "`tex' in a pair were seating neighbors in the previous session (only for non-short/special"
	local tex "`tex' sessions in same parliamentary term)."	
	local tex "`tex' Each outcome in columns (2) to (5) is a dummy variable equal to one"
	local tex "`tex' if the two MPs in a pair share the same value for the following variables:"
	local tex "`tex' (2) dummy variable equal to one if MP is male,"
	local tex "`tex' (3) dummy variable equal to one if ever held a ministerial position prior to this session,"
	local tex "`tex' (4) dummy variable equal to one if chaired a committee at any point during the previous session,"
	local tex "`tex' and (5) constituency."
	local tex "`tex' The outcomes in columns (6) to (9) are the absolute difference between the two MPs"
	local tex "`tex' in a pair for the following variables:"
	local tex "`tex' (6) age in years as of the start of the current session,"
	local tex "`tex' (7) number of sessions since first session as Althingi member,"
	local tex "`tex' (8) wages received in Althingi during the calendar year prior to the current session,"
	local tex "`tex' and (9) expenses claimed in Althingi during the calendar year prior to the current session."
	local tex "`tex' The waves and expenses data are only available from session 136 (2008/9) onwards."		
	local tex "`tex' `neighbor' `samehet'"
	local tex "`tex' `dyadSquare' `riCurly'"
	local tex "`tex' `specShortDrop'" 
	local tex "`tex' `strataDef' `plab'}"
	local tex "`tex' \\ \end{tabular} }"
	
	esttab Het1 Het2 Het3 Het4 Het5 Het6 Het7 Het8 Het9 using "`tablefolder'//pairBalance`Hetero'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("" "Same..." "Difference in...", pattern(0 1 0 0 0 1 0 0 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Neighbor\\(t-1)\\(1)}" ///
	"\specialcell{\\Gender\\(2)}" ///
	"\specialcell{Ever\\Minister\\(3)}" ///
	"\specialcell{Committee\\(t-1)\\(4)}" ///
	"\specialcell{\\Constit.\\(5)}" ///	
	"\specialcell{\\Age\\(6)}" ///
	"\specialcell{Sessions\\Experience\\(7)}" ///
	"\specialcell{Wages\\(t-1)\\(8)}" ///
	"\specialcell{Expenses\\(t-1)\\(9)}")			
	
	timer off 14
	disp "DONE dyadBalance"
	timer list 14

}
if `mpBalance'==1 { // MP-session-level balance checks

	timer on 15
	use "`votingfolder'/MP_session_analysis", clear

	// Define sessions that we will use
	g sampleSession = (!specialSession) & (!shortSession) & !(session_id == 149)
	g sampleSessionLag = !inlist(session_id,115,119,120,124,125,129,130,134,135,137,138,142,143,146,147,148,149)	

	// Initialise some table rows
	local seRow = ""
	local pRow = ""
	local pRIRow = ""
	local obsRow = ""
	local YRow = ""
	local meanRow = ""
	
	// Loop of pre-determined variables	
	local i=1
	foreach var of varlist fracOth`Hetero'NeighborRI0Lag male age reykjavik everministerPast ///
			evercommitteechairLast sessExper wages expenses {
			
		areg `var' fracOther`Hetero'NeighborRI0 if sampleSession, absorb(strataSessionPartyFE) cluster(MP_id)
		est sto BM`i'
		local obs`i' = e(N)
		qui su `var' if e(sample)==1
		local mean`i' = string(r(mean),"%9.2g")
					
		// prepare coefficient, standard error, p-value for export
		local coef_`i'_av = _b[fracOther`Hetero'NeighborRI0]
		local se_`i'_av = string(_se[fracOther`Hetero'NeighborRI0],"%9.2g")
		local pval_`i'_av = string(ttail(e(df_r),abs(`coef_`i'_av'/`se_`i'_av'))*2,"%9.2g")	
		
		// prep tex code for table
		local seRow = "`seRow' & (`se_`i'_av')"
		local obsRow = "`obsRow' & `obs`i''"
		local YRow = "`YRow' & Y"
		local meanRow = "`meanRow' & `mean`i''"
		
		// randomization inference p-values
		if `RI'==1 {
			local Nbeta_extreme = 0
			forv j=1/`runsRI' {
				areg `var' fracOther`Hetero'NeighborRI`j' if sampleSession, absorb(strataSessionPartyFE)
				local absCoef = abs(_b[fracOther`Hetero'NeighborRI`j'])
				local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`i'_av')) // + 1 every time see more extreme beta
			}
			local pval_`i'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
		}
		else {
			local pval_`i'_RI = 999
		}	
		
		// get stars for all pvals
		foreach y in av RI {
			local pstar_`i'_`y' = "" // reset p-value
			if `pval_`i'_`y''<0.10 & `pval_`i'_`y''>=0.05 local pstar_`i'_`y' = "*"
			if `pval_`i'_`y''<0.05 & `pval_`i'_`y''>=0.01 local pstar_`i'_`y' = "**"
			if `pval_`i'_`y''<0.01 local pstar_`i'_`y' = "***"
		}	
		
		// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
		if `pval_`i'_av'<0.001 local pval_`i'_av = "<0.001" 
		
		// prep tex code for table
		local pRow = "`pRow' & [`pval_`i'_av']`pstar_`i'_av'"		
		local pRIRow = "`pRIRow' & \{`pval_`i'_RI'\}`pstar_`i'_RI'"
		
		local i=`i'+1
		
	}
	
	// Output Main Effects Table
	estout BM1 BM2 BM3 BM4 BM5 BM6 BM7 BM8 BM9 using "`tablefolder'//mpBalance`Hetero'A.tex", style(tex) replace ///
	keep(fracOther`Hetero'NeighborRI0) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(fracOther`Hetero'NeighborRI0 "Proportion Other-`Hetero' Neighbor")

	local tex "\input{mpBalance`Hetero'A} \vspace{-1.2em} \\"
	local tex "`tex' `seRow' \\"
	local tex "`tex' `pRow' \\"
	local tex "`tex' `pRIRow' \\ \\[-5pt] \hline"	
	
	local tex "`tex' Observations `obsRow' \\"
	local tex "`tex' Session $\times$ Party $\times$ Strata FE `YRow' \\"
	local tex "`tex' Outcome Mean `meanRow' \\ \toprule" 
	
	local tex "`tex' \multicolumn{10}{p{23.8cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' Outcome variables are:"
	local tex "`tex' (1) proportion other-party neighbor in previous session (only for non-short/special sessions in same parliamentary term),"	
	local tex "`tex' (2) dummy variable equal to one if MP is male,"
	local tex "`tex' (3) age in years as of the start of the current session,"
	local tex "`tex' (4) dummy variable equal to one if elected from Reykjavik constituency (North or South from session 129 (2003) onwards),"
	local tex "`tex' (5) dummy variable equal to one if ever held a ministerial position prior to this session,"
	local tex "`tex' (6) dummy variable equal to one if chaired a committee at any point during the previous session,"
	local tex "`tex' (7) number of sessions since first session as Althingi member."
	local tex "`tex' The outcomes for columns (8) and (9) are, respectively, the wages received,"
	local tex "`tex' and other expenses claimed in Althingi during the calendar year prior to the current session,"
	local tex "`tex' with the data for these variables only available from session 136 (2008/9) onwards."
	local tex "`tex' `propothMP'"
	local tex "`tex' `mpSEp' `riCurly'"
	local tex "`tex' `specShortDrop'" 
	local tex "`tex' `strataDefMP' `plab'}"	
	local tex "`tex' \\ \end{tabular} }"
		
	esttab BM1 BM2 BM3 BM4 BM5 BM6 BM7 BM8 BM9 using "`tablefolder'//mpBalance`Hetero'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mtitles("\specialcell{Proportion\\Oth-Party\\Neighbor (t-1)\\(1)}" ///
	"\specialcell{\\Male\\(2)}" "\specialcell{\\Age\\(3)}" ///
	"\specialcell{Reykjavik\\Constit.\\(4)}" ///	
	"\specialcell{Ever\\Minister\\(5)}" ///
	"\specialcell{Committee\\(t-1)\\(6)}" "\specialcell{Sessions\\Experience\\(7)}" ///
	"\specialcell{Wages\\(t-1)\\(8)}" "\specialcell{Expenses\\(t-1)\\(9)}")		
			
	timer off 15
	disp "DONE mpBalance"
	timer list 15			
}
if `dyadTrends'==1 { // Trends in Party Discipline (pair-level)

	timer on 16

	use `datamod', clear
	keep session_id samevote?c* votediff?c* voteSim?c* *RI* minMP maxMP pairStrata samecoalition sameparty ///
	samegender specialSession shortSession firstSession lastSession leadShortIncomplete	party_id party_id_d	///
	sessionXpairStrataXpartyPair samevotePresent samevote4cNoAbsent omni_coany
	
	// fix session_ids so that regular sessions have consecutive numbering
	g session_id_recode = session_id
	replace session_id_recode = session_id-1 if session_id>=120 & session_id<=123
	replace session_id_recode = session_id-2 if session_id>=125 & session_id<=128
	replace session_id_recode = session_id-3 if session_id>=130 & session_id<=133
	replace session_id_recode = session_id-4 if session_id>=135 & session_id<=136
	replace session_id_recode = session_id-5 if session_id>=138 & session_id<=141
	replace session_id_recode = session_id-6 if session_id>=143 & session_id<=146
	replace session_id_recode = session_id-7 if session_id==148
	
	// re-label sessions using new coding
	la de sessions2 115 "1991-92" 116 "1992-93" 117 "1993-94" 118 "1994-95" 119 "1995-96" 120 "1996-97" ///
		121 "1997-98" 122 "1998-99" 123 "1999-00" 124 "2000-01" 125 "2001-02" 126 "2002-03" ///
		127 "2003-04" 128 "2004-05" 129 "2005-06" 130 "2006-07" 131 "2007-08" 132 "2008-09" ///
		133 "2009-10" 134 "2010-11" 135 "2011-12" 136 "2012-13" 137 "2013-14" 138 "2014-15" 139 "2015-16" ///
		140 "2016-17" 141 "2017-18" 142 "2018-19"
	la val session_id_recode sessions2
	drop session_id	
	
	// loop over sample splits
	foreach x in party coalition gender {

		local X = proper("`x'")
		
		// Collapse voting outcomes to session-sameX-level
		preserve
			collapse samevote4c voteSim4c samevotePresent samevote4cNoAbsent omni_coany, by(session_id same`x')
			
			// loop over our two main outcomes
			local trendlabs `""Fraction of Pairs That Vote the Same Way" "Average Vote Similarity" "Fraction YY, NN, or Abstain-Abstain" "Fraction Vote Same Way (No Absence)" "Average Co-Sponsorship Links""'
			local i=1
			foreach var of varlist samevote4c voteSim4c samevotePresent samevote4cNoAbsent omni_coany {
			
				// outcome label
				local trendlab: word `i' of `trendlabs'
				
				// modify the y-axis range for NoAbsent outcome
				if `i'==4 {
					local yrange = "yscale(r(0.76 1))"
					local yrange2 = "0.6(0.1)1"
				}
				else {
					local yrange = ""
					local yrange2 = ""
				}

				// prep numbers for positioning of graph text
				qui su `var' if session_id_recode<=127
				local max = r(max)+0.02
				local min = r(min)-0.02
				
				// now plot trends
				line `var' session_id_recode if same`x'==0, ///
					lcolor(black) || ///
				line `var' session_id_recode if same`x'==1, ///
					lcolor(sea) lpattern(dash) ///
					xlabel(115(2)141, angle(45) val noticks labsize(3.5)) ///
					ylabel(`yrange2', angle(horiontal) grid labsize(3.5)) `yrange' ///
					ytitle("`trendlab'", size(4.5)) ///
					xtitle("") ///
					text(`max' 120 "Same-`X' Pairs", size(3.5) color(sea)) /// 
					text(`min' 120 "Different-`X' Pairs", size(3.5) color(black)) /// 			
					legend(off)
				graph export "`graphfolder'/trends/pair`var'`X'.pdf", replace
				
				local i=`i'+1
			}
		restore
	}
	
	timer off 16
	disp "DONE dyadTrends"
	timer list 16	
	
}
if `mpTrends'==1 { // Trends in Party Discipline (MP-level)

	timer on 17
	use "`votingfolder'/MP_session_analysis", clear
	keep if (!specialSession) & (!shortSession) & !(session_id == 149)
	
	// fix session_ids so that regular sessions have consecutive numbering
	g session_id_recode = session_id
	replace session_id_recode = session_id-1 if session_id>=120 & session_id<=123
	replace session_id_recode = session_id-2 if session_id>=125 & session_id<=128
	replace session_id_recode = session_id-3 if session_id>=130 & session_id<=133
	replace session_id_recode = session_id-4 if session_id>=135 & session_id<=136
	replace session_id_recode = session_id-5 if session_id>=138 & session_id<=141
	replace session_id_recode = session_id-6 if session_id>=143 & session_id<=146
	replace session_id_recode = session_id-7 if session_id==148
	
	// re-label sessions using new coding
	la de sessions2 115 "1991-92" 116 "1992-93" 117 "1993-94" 118 "1994-95" 119 "1995-96" 120 "1996-97" ///
		121 "1997-98" 122 "1998-99" 123 "1999-00" 124 "2000-01" 125 "2001-02" 126 "2002-03" ///
		127 "2003-04" 128 "2004-05" 129 "2005-06" 130 "2006-07" 131 "2007-08" 132 "2008-09" ///
		133 "2009-10" 134 "2010-11" 135 "2011-12" 136 "2012-13" 137 "2013-14" 138 "2014-15" 139 "2015-16" ///
		140 "2016-17" 141 "2017-18" 142 "2018-19"
	la val session_id_recode sessions2
	drop session_id		
		
	// Collapse voting outcomes to session-sameX-level
	collapse non_compliance voteDiff4c cospon_all cosponSame_all rebelStrong rebelWeak, by(session_id)
	replace rebelStrong = rebelStrong*100
	replace rebelWeak = rebelWeak*100
		
	// plot leader non-compliance
	line non_compliance session_id_recode, ///
		xlabel(115(2)141, angle(45) val noticks labsize(3.5)) ///
		ylabel(0.1(0.1)0.5, angle(horiontal) grid labsize(3.5)) ///
		ytitle("Average Leader Non-Compliance", size(4.5)) ///
		xtitle("Session", size(4.5)) ///
		legend(off)
	graph export "`graphfolder'/trends/mpNoncompliance.pdf", replace
	
	// plot rebellion
	line rebelStrong session_id_recode, color(black) || ///
		line rebelWeak session_id_recode, color(sea) lpattern(dash) ///
		xlabel(115(2)141, angle(45) val noticks labsize(3.5)) ///
		ylabel(0(0.2)1.2, angle(horiontal) grid labsize(3.5)) ///
		ytitle("Rebellion Percentage", size(4.5)) ///
		text(1.05 137 "Rebel Rate", size(3.5) color(sea)) /// 
		text(0.3 138 "Strong" "Rebel Rate", size(3.5) color(black)) ///			
		xtitle("") ///
		legend(off)
	graph export "`graphfolder'/trends/mpRebel.pdf", replace
	
	// prep numbers for positioning of graph text
	qui su cosponSame if session_id_recode<=127
	local max = r(max)+10
	local min = r(min)-15	
	
	// plot co-sponsorship separately by same- vs. different-party
	line cospon_all session_id_recode, color(black) || ///
		line cosponSame_all session_id_recode, color(sea) lpattern(dash) ///
		xlabel(115(2)141, angle(45) val noticks labsize(3.5)) ///
		ylabel(, angle(horiontal) grid labsize(3.5)) ///
		ytitle("Average Co-Sponsorship Links", size(4.5)) ///
		xtitle("Session", size(4.5)) ///
		text(`max' 122 "Same-Party Links", size(3.5) color(sea)) /// 
		text(`min' 122 "Different-Party Links", size(3.5) color(black)) ///		
		legend(off)
	graph export "`graphfolder'/trends/mpCospon.pdf", replace	
	
	timer off 17
	disp "DONE mpTrends"
	timer list 17		
}
if `voteCalendar'==1 { // Visualise number of votes across time

timer on 21

// Store regular sessions only
use "`votingfolder'/MP_session_analysis", clear
keep if (!specialSession) & (!shortSession) & !(session_id == 149)
keep session_id
duplicates drop
g sampleSession = 1
save `regSess'

// Bring voting data created from Iceland_voting_prep
use "`votingfolder'/voting_analysis", clear

// Keep only necessary vote-level variables
keep vote_id vote session_id bill_id votedate
g absent=vote=="absent"
bys vote_id: egen absentPropVote = mean(absent)
keep vote_id session_id bill_id votedate absentPropVote
duplicates drop
isid vote_id

// Absence at date-level
bys votedate: egen absentPercToday = mean(absentPropVote)
replace absentPercToday = 100*absentPercToday
drop absentPropVote

// Get number of votes per day
bys votedate: g NvotesToday = _N
keep session_id votedate NvotesToday absentPercToday
duplicates drop
isid votedate
save `calendar'

// Session dates: https://www.althingi.is/thingmenn/althingismannatal/thingsetutimi-numer-loggjafarthinga/
// 1991/92 (115): Oct 1, 1991
// 2017/18 (148): July 18, 2018 (assuming 2017 is typo in above URL)
local days = mdy(7,18,2018)-mdy(10,1,1991)+1
clear
set obs `days'
g votedate = _n + mdy(10,1,1991) - 1
format votedate %d
merge 1:1 votedate using `calendar', keep(1 3) nogen

// Fill in missing session dates
replace session_id=115 if inrange(votedate,mdy(10,1,1991),mdy(5,20,1992)) & session_id==.
replace session_id=116 if inrange(votedate,mdy(8,17,1992),mdy(5,9,1993)) & session_id==.
replace session_id=117 if inrange(votedate,mdy(10,1,1993),mdy(6,17,1994)) & session_id==.
replace session_id=118 if inrange(votedate,mdy(10,1,1994),mdy(2,25,1995)) & session_id==.
replace session_id=119 if inrange(votedate,mdy(5,16,1995),mdy(6,15,1995)) & session_id==.
replace session_id=120 if inrange(votedate,mdy(10,2,1995),mdy(6,5,1996)) & session_id==.
replace session_id=121 if inrange(votedate,mdy(10,1,1996),mdy(5,17,1997)) & session_id==.
replace session_id=122 if inrange(votedate,mdy(10,1,1997),mdy(6,5,1998)) & session_id==.
replace session_id=123 if inrange(votedate,mdy(10,1,1998),mdy(3,25,1999)) & session_id==.
replace session_id=124 if inrange(votedate,mdy(6,8,1999),mdy(6,16,1999)) & session_id==.
replace session_id=125 if inrange(votedate,mdy(10,1,1999),mdy(7,2,2000)) & session_id==.
replace session_id=126 if inrange(votedate,mdy(10,2,2000),mdy(5,20,2001)) & session_id==.
replace session_id=127 if inrange(votedate,mdy(10,1,2001),mdy(5,3,2002)) & session_id==.
replace session_id=128 if inrange(votedate,mdy(10,1,2002),mdy(3,15,2003)) & session_id==.
replace session_id=129 if inrange(votedate,mdy(5,26,2003),mdy(5,27,2003)) & session_id==.
replace session_id=130 if inrange(votedate,mdy(10,1,2003),mdy(7,22,2004)) & session_id==.
replace session_id=131 if inrange(votedate,mdy(10,1,2004),mdy(5,11,2005)) & session_id==.
replace session_id=132 if inrange(votedate,mdy(10,1,2005),mdy(6,3,2006)) & session_id==.
replace session_id=133 if inrange(votedate,mdy(10,2,2006),mdy(3,18,2007)) & session_id==.
replace session_id=134 if inrange(votedate,mdy(5,31,2007),mdy(6,13,2007)) & session_id==.
replace session_id=135 if inrange(votedate,mdy(10,1,2007),mdy(9,12,2008)) & session_id==.
replace session_id=136 if inrange(votedate,mdy(10,1,2008),mdy(4,17,2009)) & session_id==.
replace session_id=137 if inrange(votedate,mdy(5,15,2009),mdy(8,28,2009)) & session_id==.
replace session_id=138 if inrange(votedate,mdy(10,1,2009),mdy(9,28,2010)) & session_id==.
replace session_id=139 if inrange(votedate,mdy(10,1,2010),mdy(9,17,2011)) & session_id==.
replace session_id=140 if inrange(votedate,mdy(10,1,2011),mdy(6,19,2012)) & session_id==.
replace session_id=141 if inrange(votedate,mdy(9,11,2012),mdy(3,28,2013)) & session_id==.
replace session_id=142 if inrange(votedate,mdy(6,6,2013),mdy(7,5,2013)) & session_id==.
replace session_id=143 if inrange(votedate,mdy(10,1,2013),mdy(6,18,2014)) & session_id==.
replace session_id=144 if inrange(votedate,mdy(9,9,2014),mdy(7,3,2015)) & session_id==.
replace session_id=145 if inrange(votedate,mdy(9,8,2015),mdy(10,13,2016)) & session_id==.
replace session_id=146 if inrange(votedate,mdy(12,6,2016),mdy(6,1,2017)) & session_id==.
replace session_id=147 if inrange(votedate,mdy(9,12,2017),mdy(9,27,2017)) & session_id==.
replace session_id=148 if inrange(votedate,mdy(12,14,2017),mdy(7,18,2018)) & session_id==.

// Get each session's end date
bys session_id: egen sessEndDate = max(votedate)

// Merge on regular session indicator
merge m:1 session_id using `regSess', assert(1 3) nogen
keep if session_id!=. & sampleSession==1
drop sampleSession
replace NvotesToday=0 if NvotesToday==.

// fix session_ids so that regular sessions have consecutive numbering
g session_id_recode = session_id
replace session_id_recode = session_id-1 if session_id>=120 & session_id<=123
replace session_id_recode = session_id-2 if session_id>=125 & session_id<=128
replace session_id_recode = session_id-3 if session_id>=130 & session_id<=133
replace session_id_recode = session_id-4 if session_id>=135 & session_id<=136
replace session_id_recode = session_id-5 if session_id>=138 & session_id<=141
replace session_id_recode = session_id-6 if session_id>=143 & session_id<=146
replace session_id_recode = session_id-7 if session_id==148

// re-label sessions using new coding
la de sessions2 115 "1991-92" 116 "1992-93" 117 "1993-94" 118 "1994-95" 119 "1995-96" 120 "1996-97" ///
	121 "1997-98" 122 "1998-99" 123 "1999-00" 124 "2000-01" 125 "2001-02" 126 "2002-03" ///
	127 "2003-04" 128 "2004-05" 129 "2005-06" 130 "2006-07" 131 "2007-08" 132 "2008-09" ///
	133 "2009-10" 134 "2010-11" 135 "2011-12" 136 "2012-13" 137 "2013-14" 138 "2014-15" 139 "2015-16" ///
	140 "2016-17" 141 "2017-18" 142 "2018-19"
la val session_id_recode sessions2
drop session_id	
decode session_id_recode, g(session_id_txt)

// Get session midpoints
local xlabs = ""
local year = 1991
foreach x in 115 116 117 118 119 120 121 122 123 124 125 126 ///
	127 128 129 130 131 132 133 134 135 136 137 138 139 ///
	140 141 {

	qui su votedate if session_id_recode==`x'	
	local mid`year' = round(r(mean),1)
	local year = `year'+1
}

// Clean and plot
scatter NvotesToday votedate if NvotesToday!=0, ///
	ytitle("Number of Votes", size(5)) ///
	xtitle("") xsize(15) ysize(5) ///
	ylabel(, labsize(3.5)) ///
	xlabel(`mid1991' "1991-92" `mid1992' "1992-93" `mid1993' "1993-94" `mid1994' "1994-95" ///
			`mid1995' "1995-96" `mid1996' "1996-97" `mid1997' "1997-98" `mid1998' "1998-99" ///
			`mid1999' "1999-00" `mid2000' "2000-01" `mid2001' "2001-02" `mid2002' "2002-03" ///
			`mid2003' "2003-04" `mid2004' "2004-05" `mid2005' "2005-06" `mid2006' "2006-07" ///
			`mid2007' "2007-08" `mid2008' "2008-09" `mid2009' "2009-10" `mid2010' "2010-11" ///
			`mid2011' "2011-12" `mid2012' "2012-13" `mid2013' "2013-14" `mid2014' "2014-15" ///
			`mid2015' "2015-16" `mid2016' "2016-17" `mid2017' "2017-18", angle(45) val noticks labsize(3.5))
graph export "`graphfolder'/calendar/votesCalendar.pdf", replace

// First break for each session
g breakDate=mdy(12,22,1991) if session_id_txt=="1991-92"
replace breakDate=mdy(12,22,1992) if session_id_txt=="1992-93"
replace breakDate=mdy(12,21,1993) if session_id_txt=="1993-94"
replace breakDate=mdy(12,30,1994) if session_id_txt=="1994-95"
replace breakDate=mdy(12,22,1995) if session_id_txt=="1995-96"
replace breakDate=mdy(12,20,1996) if session_id_txt=="1996-97"
replace breakDate=mdy(12,20,1997) if session_id_txt=="1997-98"
replace breakDate=mdy(12,20,1998) if session_id_txt=="1998-99"
replace breakDate=mdy(12,21,1999) if session_id_txt=="1999-00"
replace breakDate=mdy(12,16,2000) if session_id_txt=="2000-01"
replace breakDate=mdy(12,24,2001) if session_id_txt=="2001-02"
replace breakDate=mdy(12,13,2002) if session_id_txt=="2002-03"
replace breakDate=mdy(12,15,2003) if session_id_txt=="2003-04"
replace breakDate=mdy(12,10,2004) if session_id_txt=="2004-05"
replace breakDate=mdy(12,9,2005) if session_id_txt=="2005-06"
replace breakDate=mdy(12,9,2006) if session_id_txt=="2006-07"
replace breakDate=mdy(12,14,2007) if session_id_txt=="2007-08"
replace breakDate=mdy(12,22,2008) if session_id_txt=="2008-09"
replace breakDate=mdy(12,30,2009) if session_id_txt=="2009-10"
replace breakDate=mdy(12,18,2010) if session_id_txt=="2010-11"
replace breakDate=mdy(12,17,2011) if session_id_txt=="2011-12"
replace breakDate=mdy(12,22,2012) if session_id_txt=="2012-13"
replace breakDate=mdy(12,21,2013) if session_id_txt=="2013-14"
replace breakDate=mdy(12,16,2014) if session_id_txt=="2014-15"
replace breakDate=mdy(12,19,2015) if session_id_txt=="2015-16"
replace breakDate=mdy(12,22,2016) if session_id_txt=="2016-17"
replace breakDate=mdy(12,30,2017) if session_id_txt=="2017-18"

// Second break for each session
g breakDate2=mdy(1,14,1993) if session_id_txt=="1992-93"
replace breakDate2=mdy(5,11,1994) if session_id_txt=="1993-94"
replace breakDate2=mdy(1,13,1999) if session_id_txt=="1998-99"
replace breakDate2=mdy(5,13,2000) if session_id_txt=="1999-00"
replace breakDate2=mdy(1,24,2001) if session_id_txt=="2000-01"
replace breakDate2=mdy(5,28,2004) if session_id_txt=="2003-04"
replace breakDate2=mdy(5,4,2006) if session_id_txt=="2005-06"
replace breakDate2=mdy(5,29,2008) if session_id_txt=="2007-08"
replace breakDate2=mdy(1,8,2010) if session_id_txt=="2009-10"
replace breakDate2=mdy(6,15,2011) if session_id_txt=="2010-11"
replace breakDate2=mdy(5,16,2014) if session_id_txt=="2013-14"
replace breakDate2=mdy(6,2,2016) if session_id_txt=="2015-16"
replace breakDate2=mdy(6,12,2017) if session_id_txt=="2017-18"

// Third break for each session
g breakDate3=mdy(3,11,1999) if session_id_txt=="1998-99"
replace breakDate3=mdy(6,24,2010) if session_id_txt=="2009-10"
replace breakDate3=mdy(6,8,2016) if session_id_txt=="2015-16"

// Plot session-by-session
g top=400
g zero=0
scatter NvotesToday votedate if NvotesToday!=0, ///
	by(session_id_recode, xr noixt noixl note("Grey lines indicate last day before a break. Red lines indicate last day of session.") legend(off)) ///
	ytitle("Number of Votes", size(5)) ///
	xtitle("") ///
	ylabel(, labsize(3.5)) ///
	|| rspike zero top breakDate, lw(thin) lcolor(black) ///
	|| rspike zero top breakDate2, lw(thin) lcolor(black) ///
	|| rspike zero top breakDate3, lw(thin) lcolor(black) ///	
	|| rspike zero top sessEndDate, lw(thin) lcolor(red)
graph export "`graphfolder'/calendar/votesCalendarBySess.pdf", replace	

// Daily voting absence vs. number of votes on that day
binsreg absentPercToday NvotesToday, ///
	ytitle("Voting Absence (%)", size(5)) ///
	xtitle("Number of Votes Held on a Day", size(5)) ///
	xlabel(, labsize(3.5)) ylabel(, labsize(3.5))
graph export "`graphfolder'/calendar/absenceVnumvotes.pdf", replace	

// Calculate basic stats
qui su NvotesToday
local TotVotes = r(mean)*r(N)
bys session_id_recode: g nvals = _n==1
count if nvals==1
local SessVotes = round(`TotVotes'/r(N),1)
drop nvals
su NvotesToday if NvotesToday>=50
local TotVotes50 = r(mean)*r(N)
local FracVotes50 = round(100*(`TotVotes50'/`TotVotes'),1)

// Display stats
di "Number of votes = `TotVotes' // Votes per session = `SessVotes' // % of votes on days with at least 50 votes = `FracVotes50'"

timer off 21
disp "DONE voteCalendar"
timer list 21

}
if `dyadVoteCrisis'==1 { // See if less cross-party cue-taking once cross-party consensus breaks down

	timer on 23

	forv r = 1/4 {	// Type of voting/cosponsorship measure?
		local measure: word `r' of "samevote4c" "samevote4c" "voteSim4c" "voteSim4c"
			
			dis "`measure'"
			use `datamod', clear
			
			if `r'==1 | `r'==3 {
				keep if session_id<=137 // before 2009/10
			}
			else {
				keep if session_id>=138 // 2009/10 onwards
			}
							
			keep session_id samevote?c* voteSim?c* *RI* minMP maxMP pairStrata samecoalition sameparty ///
			samegender specialSession shortSession firstSession lastSession leadShortIncomplete	party_id party_id_d	///
			sessionXpairStrataXpartyPair
			
			/* Neighborhood effect */
			
			// residualize
			qui areg `measure' if !mi(`measure') & !mi(neighborRI0) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
			local numN = e(N)
			local numDummy = e(df_a) 
			predict yresid, residuals
			qui areg neighborRI0 if !mi(`measure') & !mi(neighborRI0) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
			local numN2 = e(N)
			predict xresid, residuals
			assert `numN' == `numN2'
			
			// run regression
			xi: regdyad2 yresid xresid if !mi(`measure') & !mi(neighborRI0) & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d), ///
				dyads(minMP maxMP)
			est sto M`r'
			local obs`r' = e(N)
			qui su `measure' if e(sample)==1
			local mean`r' = string(r(mean),"%9.2g")
			local sd`r' = string(r(sd),"%9.2g")
			local sd`r'raw = `r(sd)'
			local numReg = 1
			
			// prepare coefficient, standard error, p-value for export
			local coef_`r'_av = _b[xresid]
			local se_`r'_av = string(_se[xresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1)),"%9.2g")
			local t_df = `numN'-`numReg'-`numDummy'-1
			local pval_`r'_av = string(ttail(`t_df',abs(`coef_`r'_av'/`se_`r'_av'))*2,"%9.2g")					
			drop yresid xresid	
			
			// randomization inference p-values
			if `RI'==1 {
				local Nbeta_extreme = 0
				forv i=1/`runsRI' {
					areg `measure' neighborRI`i', absorb(sessionXpairStrataXpartyPair)
					local absCoef = abs(_b[neighborRI`i'])
					local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`r'_av')) // + 1 every time see more extreme beta
				}
				local pval_`r'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
			}
			else {
				local pval_`r'_RI = 999
			}			
				
			/* Hetero: party ingroup and outgroup effect */
			
			// residualize			
			qui areg `measure' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair)
			local numN = e(N)
			local numDummy = e(df_a) 
			predict yresid, residuals
			
			qui areg neighborRI0Xsame`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
			local numN2 = e(N)
			predict xinresid, residuals
			assert `numN' == `numN2'
			qui areg neighborRI0Xdiff`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
			predict xoutresid, residuals
			
			// run regression
			xi: regdyad2 yresid xinresid xoutresid if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') ///
				& !mi(pairStrata) & !mi(party_id) & !mi(party_id_d), dyads(minMP maxMP)
			est sto Het`r'
			local obsHet`r' = e(N)
			local numReg = 2
			matrix varMatrix = e(V)		
				
			// prepare coefficient, standard error, p-value for export					
			local coef_`r'_in = string(_b[xinresid],"%9.2g")
			local coef_`r'_out = string(_b[xoutresid],"%9.2g")
			local se_`r'_in = _se[xinresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
			local se_`r'_out = _se[xoutresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
			local t_df = `numN'-`numReg'-`numDummy'-1
			local pval_`r'_in =  string(ttail(`t_df',abs(`coef_`r'_in'/`se_`r'_in'))*2,"%9.2g")
			local pval_`r'_out =  string(ttail(`t_df',abs(`coef_`r'_out'/`se_`r'_out'))*2,"%9.2g")
				
			// pval calculation for the difference
			local diff = `coef_`r'_in' - `coef_`r'_out'
			local se_`r'_diff = sqrt(varMatrix[1,1]+varMatrix[2,2]-2*varMatrix[2,1])
			local pval_`r'_diff =  string(ttail(`t_df',abs(`diff'/`se_`r'_diff'))*2,"%9.2g")
			drop yresid xinresid xoutresid
			
			// randomization inference p-values
			if `RI'==1 {
				local Nbeta_extreme_in = 0
				local Nbeta_extreme_out = 0
				local Nbeta_extreme_diff = 0
				forv i=1/`runsRI' {
					areg `measure' neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero', absorb(sessionXpairStrataXpartyPair)
					local absCoefIn = abs(_b[neighborRI`i'Xsame`hetero'])
					local absCoefOut = abs(_b[neighborRI`i'Xdiff`hetero'])
					local Nbeta_extreme_in = `Nbeta_extreme_in' + (`absCoefIn' >= abs(`coef_`r'_in')) // + 1 every time see more extreme beta
					local Nbeta_extreme_out = `Nbeta_extreme_out' + (`absCoefOut' >= abs(`coef_`r'_out')) 
				
					// for difference between in vs out p-value we need separate regressions, using "modified" outcome
					g `measure'_m = `measure'
					replace `measure'_m = `measure'_m + `coef_`r'_av' if neighborRI`i' == 1 & neighborRI0 == 0
					replace `measure'_m = `measure'_m - `coef_`r'_av' if neighborRI`i' == 0 & neighborRI0 == 1	
					
					// run regression with modified outcome
					areg `measure'_m neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero', absorb(sessionXpairStrataXpartyPair)
					local absCoefDiff = abs(_b[neighborRI`i'Xsame`hetero']-_b[neighborRI`i'Xdiff`hetero'])
					local Nbeta_extreme_diff = `Nbeta_extreme_diff' + (`absCoefDiff' >= abs(`diff')) 
					drop `measure'_m
				}
				local pval_`r'_RIin = string(`Nbeta_extreme_in'/`runsRI',"%9.2g")
				local pval_`r'_RIout = string(`Nbeta_extreme_out'/`runsRI',"%9.2g")	
				local pval_`r'_RIdiff = string(`Nbeta_extreme_diff'/`runsRI',"%9.2g")	
			}
			else {
				local pval_`r'_RIin = 999
				local pval_`r'_RIout = 999	
				local pval_`r'_RIdiff = 999										
			}					
			
			// get stars for all pvals
			foreach y in av in out diff RI RIin RIout RIdiff {
				local pstar_`r'_`y' = "" // reset p-value
				if `pval_`r'_`y''<0.10 & `pval_`r'_`y''>=0.05 local pstar_`r'_`y' = "*"
				if `pval_`r'_`y''<0.05 & `pval_`r'_`y''>=0.01 local pstar_`r'_`y' = "**"
				if `pval_`r'_`y''<0.01 local pstar_`r'_`y' = "***"
			}
			
			// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
			foreach y in av in out diff {
				if `pval_`r'_`y''<0.001 local pval_`r'_`y' = "<0.001" 
			}				
	}
		
	// Output Heterogeneous Effects Table
	local tex "Neighbor $\times$ Different `Hetero' & `coef_1_out' & `coef_2_out' & `coef_3_out' & `coef_4_out' \\"
	local tex "`tex' `bipart' & [`pval_1_out']`pstar_1_out' & [`pval_2_out']`pstar_2_out' & [`pval_3_out']`pstar_3_out' & [`pval_4_out']`pstar_4_out' \\"
	local tex "`tex' & \{`pval_1_RIout'\}`pstar_1_RIout' & \{`pval_2_RIout'\}`pstar_2_RIout' & \{`pval_3_RIout'\}`pstar_3_RIout' & \{`pval_4_RIout'\}`pstar_4_RIout' \\ \\[-5pt]"		
	
	local tex "`tex' Neighbor $\times$ Same `Hetero' & `coef_1_in' & `coef_2_in' & `coef_3_in' & `coef_4_in' \\"
	local tex "`tex'  & [`pval_1_in']`pstar_1_in' & [`pval_2_in']`pstar_2_in' & [`pval_3_in']`pstar_3_in' & [`pval_4_in']`pstar_4_in' \\"
	local tex "`tex' & \{`pval_1_RIin'\}`pstar_1_RIin' & \{`pval_2_RIin'\}`pstar_2_RIin' & \{`pval_3_RIin'\}`pstar_3_RIin' & \{`pval_4_RIin'\}`pstar_4_RIin' \\ \\[-5pt]"		
		
	local tex "`tex' Same = Different & [`pval_1_diff']`pstar_1_diff' & [`pval_2_diff']`pstar_2_diff' & [`pval_3_diff']`pstar_3_diff' & [`pval_4_diff']`pstar_4_diff' \\"
	local tex "`tex' & \{`pval_1_RIdiff'\}`pstar_1_RIdiff' & \{`pval_2_RIdiff'\}`pstar_2_RIdiff' & \{`pval_3_RIdiff'\}`pstar_3_RIdiff' & \{`pval_4_RIdiff'\}`pstar_4_RIdiff' \\ \\[-5pt] \hline"				
	
	local tex "`tex' Observations & `obsHet1' & `obsHet2' & `obsHet3' & `obsHet4' \\" 
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `mean3' & `mean4' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sd3' & `sd4' \\ \toprule" 
	
	local tex "`tex' \multicolumn{5}{p{13.5cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `compliance' `similarity'"
	local tex "`tex' `neighbor' `samehet'"
	local tex "`tex' `dyadSquare' `riCurly'"
	local tex "`tex' `specShortDrop'" 
	local tex "`tex' `strataDef' `plab'}"
	local tex "`tex' \\ \end{tabular} }"
	
	esttab Het1 Het2 Het3 Het4 using "`tablefolder'//pairVoteCrisis`Hetero'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("Compliance" "Similarity", pattern(1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Before\\2009/10\\(1)}" "\specialcell{2009/10\\Onwards\\(2)}" ///
	"\specialcell{Before\\2009/10\\(3)}" ///	
	"\specialcell{2009/10\\Onwards\\(4)}")	
	
	timer off 23
	disp "DONE dyadVoteCrisis"
	timer list 23
}
if `dyadVoteYesNoAbs'==1 { // Dyadic voting results (yes-yes, no-no, abstain-abstain)

	timer on 24
	forv k = 1/3 { 		// Time period?
		local time: word `k' of "Lag" "" "Lead"

		forv r = 1/4 {	// Type of voting/cosponsorship measure?
			local measure: word `r' of "samevote4c" "samevotePresent" "samevotePresent_VS" "samevotePresent_VVS"
				
				dis "`measure'`time'"
				use `datamod', clear
				keep session_id samevote?c* votediff?c* voteSim?c* *RI* minMP maxMP pairStrata samecoalition sameparty ///
				samegender specialSession shortSession firstSession lastSession leadShortIncomplete	party_id party_id_d ///
				sessionXpairStrataXpartyPair samevotePresent*
				
				if `k'==1 {
					drop if firstSession==1 
				}
				else if `k'==3 {
					drop if lastSession==1
					drop if leadShortIncomplete==1
				}
				
				/* Neighborhood effect */
				
				// residualize
				qui areg `measure'`time' if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair)
				local numN = e(N)
				local numDummy = e(df_a) 
				predict yresid, residuals
				qui areg neighborRI0 if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
				local numN2 = e(N)
				predict xresid, residuals
				assert `numN' == `numN2'
				
				// run regression
				xi: regdyad2 yresid xresid if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d), ///
					dyads(minMP maxMP)
				est sto M`time'`r'
				local obs`time'`r' = e(N)
				qui su `measure'`time' if e(sample)==1
				local mean`time'`r' = string(r(mean),"%9.2g")
				local sd`time'`r' = string(r(sd),"%9.2g")
				local sd`time'`r'raw = `r(sd)'
				local numReg = 1
				
				// prepare coefficient, standard error, p-value for export
				local coef_`time'`r'_av = _b[xresid]
				local se_`time'`r'_av = string(_se[xresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1)),"%9.2g")
				local t_df = `numN'-`numReg'-`numDummy'-1
				local pval_`time'`r'_av = string(ttail(`t_df',abs(`coef_`time'`r'_av'/`se_`time'`r'_av'))*2,"%9.2g")					
				drop yresid xresid	
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme = 0
					forv i=1/`runsRI' {
						areg `measure'`time' neighborRI`i', absorb(sessionXpairStrataXpartyPair)
						local absCoef = abs(_b[neighborRI`i'])
						local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`time'`r'_av')) // + 1 every time see more extreme beta
					}
					local pval_`time'`r'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
				}
				else {
					local pval_`time'`r'_RI = 999
				}					
				
				/* Hetero: party ingroup and outgroup effect */
								
				// residualize
				qui areg `measure'`time' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure'`time') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair)
				local numN = e(N)
				local numDummy = e(df_a) 
				predict yresid, residuals
				qui areg neighborRI0Xsame`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure'`time') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
				local numN2 = e(N)
				predict xinresid, residuals
				assert `numN' == `numN2'
				qui areg neighborRI0Xdiff`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure'`time') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
				predict xoutresid, residuals
				
				// run regression
				xi: regdyad2 yresid xinresid xoutresid if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure'`time') ///
					& !mi(pairStrata) & !mi(party_id) & !mi(party_id_d), dyads(minMP maxMP)
				est sto Het`time'`r'
				local obsHet`time'`r' = e(N)
				local numReg = 2
				matrix varMatrix = e(V)
				
				// prepare coefficient, standard error, p-value for export					
				local coef_`time'`r'_in = string(_b[xinresid],"%9.2g")
				local coef_`time'`r'_out = string(_b[xoutresid],"%9.2g")
				local se_`time'`r'_in = _se[xinresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
				local se_`time'`r'_out = _se[xoutresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
				local t_df = `numN'-`numReg'-`numDummy'-1
				local pval_`time'`r'_in =  string(ttail(`t_df',abs(`coef_`time'`r'_in'/`se_`time'`r'_in'))*2,"%9.2g")
				local pval_`time'`r'_out =  string(ttail(`t_df',abs(`coef_`time'`r'_out'/`se_`time'`r'_out'))*2,"%9.2g")
				
				// pval calculation for the difference
				local diff = `coef_`time'`r'_in' - `coef_`time'`r'_out'
				local se_`time'`r'_diff = sqrt(varMatrix[1,1]+varMatrix[2,2]-2*varMatrix[2,1])
				local pval_`time'`r'_diff =  string(ttail(`t_df',abs(`diff'/`se_`time'`r'_diff'))*2,"%9.2g")
				drop yresid xinresid xoutresid
				
				/*
				// diff-party point estimate and 95% confidence interval for figure
				if `k'==2 {
					local voteYesNo_`r'_out = `coef_`r'_out'/`sd`r'raw'
					local voteYesNol_`r'_out = (`coef_`r'_out' - invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
					local voteYesNou_`r'_out = (`coef_`r'_out' + invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
				}
				*/
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme_in = 0
					local Nbeta_extreme_out = 0
					local Nbeta_extreme_diff = 0
					forv i=1/`runsRI' {
						areg `measure'`time' neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero', absorb(sessionXpairStrataXpartyPair)
						local absCoefIn = abs(_b[neighborRI`i'Xsame`hetero'])
						local absCoefOut = abs(_b[neighborRI`i'Xdiff`hetero'])
						local Nbeta_extreme_in = `Nbeta_extreme_in' + (`absCoefIn' >= abs(`coef_`time'`r'_in')) // + 1 every time see more extreme beta
						local Nbeta_extreme_out = `Nbeta_extreme_out' + (`absCoefOut' >= abs(`coef_`time'`r'_out')) 
					
						// for difference between in vs out p-value we need separate regressions, using "modified" outcome
						g `measure'`time'_m = `measure'`time'
						replace `measure'`time'_m = `measure'`time'_m + `coef_`time'`r'_av' if neighborRI`i' == 1 & neighborRI0 == 0
						replace `measure'`time'_m = `measure'`time'_m - `coef_`time'`r'_av' if neighborRI`i' == 0 & neighborRI0 == 1	
						
						// run regression with modified outcome
						areg `measure'`time'_m neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero', absorb(sessionXpairStrataXpartyPair)
						local absCoefDiff = abs(_b[neighborRI`i'Xsame`hetero']-_b[neighborRI`i'Xdiff`hetero'])
						local Nbeta_extreme_diff = `Nbeta_extreme_diff' + (`absCoefDiff' >= abs(`diff')) 
						drop `measure'`time'_m
					}
					local pval_`time'`r'_RIin = string(`Nbeta_extreme_in'/`runsRI',"%9.2g")
					local pval_`time'`r'_RIout = string(`Nbeta_extreme_out'/`runsRI',"%9.2g")	
					local pval_`time'`r'_RIdiff = string(`Nbeta_extreme_diff'/`runsRI',"%9.2g")	
				}
				else {
					local pval_`time'`r'_RIin = 999
					local pval_`time'`r'_RIout = 999	
					local pval_`time'`r'_RIdiff = 999										
				}								
				
				// get stars for all pvals
				foreach y in av in out diff RI RIin RIout RIdiff {
					local pstar_`time'`r'_`y' = "" // reset p-value
					if `pval_`time'`r'_`y''<0.10 & `pval_`time'`r'_`y''>=0.05 local pstar_`time'`r'_`y' = "*"
					if `pval_`time'`r'_`y''<0.05 & `pval_`time'`r'_`y''>=0.01 local pstar_`time'`r'_`y' = "**"
					if `pval_`time'`r'_`y''<0.01 local pstar_`time'`r'_`y' = "***"
				}
				
				// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
				foreach y in av in out diff {
					if `pval_`time'`r'_`y''<0.001 local pval_`time'`r'_`y' = "<0.001" 
				}					
		}
	}

		
	// Output Main Effects Table (online appendix)
	estout M1 M2 M3 M4 using "`tablefolder'//pairVoteYesNoAbsA.tex", style(tex) replace ///
	keep(xresid) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(xresid "Neighbor")

	estout MLead1 MLead2 MLead3 MLead4 using "`tablefolder'//pairVoteYesNoAbsB.tex", style(tex) replace ///
	keep(xresid) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(xresid "Neighbor")

	estout MLag1 MLag2 MLag3 MLag4 using "`tablefolder'//pairVoteYesNoAbsC.tex", style(tex) replace ///
	keep(xresid) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(xresid "Neighbor")

	local tex "& \multicolumn{4}{c}{\textit{Panel A:} Contemporaneous Effect (t)} \vspace{0.2em} \\"
	local tex "`tex' \input{pairVoteYesNoAbsA} \vspace{-1.2em} \\"
	local tex "`tex' & (`se_1_av') & (`se_2_av') & (`se_3_av') & (`se_4_av') \\"
	local tex "`tex' & [`pval_1_av']`pstar_1_av' & [`pval_2_av']`pstar_2_av' & [`pval_3_av']`pstar_3_av' & [`pval_4_av']`pstar_4_av' \\"
	local tex "`tex' & \{`pval_1_RI'\}`pstar_1_RI' & \{`pval_2_RI'\}`pstar_2_RI' & \{`pval_3_RI'\}`pstar_3_RI' & \{`pval_4_RI'\}`pstar_4_RI' \\ \\[-5pt]"	
	local tex "`tex' Observations & `obs1' & `obs2' & `obs3' & `obs4' \\ \hline"
	local tex "`tex' \vspace{-8pt} & & & & \\"
	local tex "`tex' & \multicolumn{4}{c}{\textit{Panel B:} One Year Later (t+1)} \vspace{0.2em} \\"  
	local tex "`tex' \input{pairVoteYesNoAbsB} \vspace{-1.2em} \\" 
	local tex "`tex'  & (`se_Lead1_av') & (`se_Lead2_av') & (`se_Lead3_av') & (`se_Lead4_av') \\"
	local tex "`tex'  & [`pval_Lead1_av']`pstar_Lead1_av' & [`pval_Lead2_av']`pstar_Lead2_av' & [`pval_Lead3_av']`pstar_Lead3_av' & [`pval_Lead4_av']`pstar_Lead4_av' \\"
	local tex "`tex' & \{`pval_Lead1_RI'\}`pstar_Lead1_RI' & \{`pval_Lead2_RI'\}`pstar_Lead2_RI' & \{`pval_Lead3_RI'\}`pstar_Lead3_RI' & \{`pval_Lead4_RI'\}`pstar_Lead4_RI' \\ \\[-5pt]"		
	local tex "`tex' Observations & `obsLead1' & `obsLead2' & `obsLead3' & `obsLead4' \\ \hline" 
	local tex "`tex' \vspace{-8pt} & & & & \\"
	local tex "`tex' & \multicolumn{4}{c}{\textit{Panel C:} Previous Year (Placebo) (t-1)} \vspace{0.2em} \\"  
	local tex "`tex' \input{pairVoteYesNoAbsC} \vspace{-1.2em} \\"
	local tex "`tex'  & (`se_Lag1_av') & (`se_Lag2_av') & (`se_Lag3_av') & (`se_Lag4_av') \\"
	local tex "`tex'  & [`pval_Lag1_av']`pstar_Lag1_av' & [`pval_Lag2_av']`pstar_Lag2_av' & [`pval_Lag3_av']`pstar_Lag3_av' & [`pval_Lag4_av']`pstar_Lag4_av' \\"
	local tex "`tex' & \{`pval_Lag1_RI'\}`pstar_Lag1_RI' & \{`pval_Lag2_RI'\}`pstar_Lag2_RI' & \{`pval_Lag3_RI'\}`pstar_Lag3_RI' & \{`pval_Lag4_RI'\}`pstar_Lag4_RI' \\ \\[-5pt]"			
	local tex "`tex' Observations & `obsLag1' & `obsLag2' & `obsLag3' & `obsLag4' \\ \hline" 	
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `mean3' & `mean4' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sd3' & `sd4' \\ \toprule" 
	local tex "`tex' \multicolumn{5}{p{14.9cm}}{\footnotesize \textit{Notes:} `eachPanelFour'" 
	local tex "`tex' `compliance' `yynnaa' `contested' `neighbor'"
	local tex "`tex' `dyadSEp' `riCurly'"	
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `outcomeMeanA' `plab'}" 
	local tex "`tex' \\ \end{tabular} }"
	
	esttab M1 M2 M3 M4 using "`tablefolder'//pairVoteYesNoAbs.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("Compliance" "Yes-Yes/No-No/Abstain-Abstain", pattern(1 1 0 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{All\\(1)}" "\specialcell{All\\(2)}" ///
	"\specialcell{Below 50th\\(3)}" "\specialcell{Below 25th\\(4)}")	

	// Output Heterogeneous Effects Table
	local tex "& \multicolumn{4}{c}{\textit{Panel A:} Contemporaneous Effect (t)} \vspace{0.2em} \\" 
	
	local tex "`tex' Neighbor $\times$ Different `Hetero' & `coef_1_out' & `coef_2_out' & `coef_3_out' & `coef_4_out' \\"
	local tex "`tex' `bipart' & [`pval_1_out']`pstar_1_out' & [`pval_2_out']`pstar_2_out' & [`pval_3_out']`pstar_3_out' & [`pval_4_out']`pstar_4_out' \\"
	local tex "`tex' & \{`pval_1_RIout'\}`pstar_1_RIout' & \{`pval_2_RIout'\}`pstar_2_RIout' & \{`pval_3_RIout'\}`pstar_3_RIout' & \{`pval_4_RIout'\}`pstar_4_RIout' \\ \\[-5pt]"				
	
	local tex "`tex' Neighbor $\times$ Same `Hetero' & `coef_1_in' & `coef_2_in' & `coef_3_in' & `coef_4_in' \\"
	local tex "`tex'  & [`pval_1_in']`pstar_1_in' & [`pval_2_in']`pstar_2_in' & [`pval_3_in']`pstar_3_in' & [`pval_4_in']`pstar_4_in' \\"
	local tex "`tex' & \{`pval_1_RIin'\}`pstar_1_RIin' & \{`pval_2_RIin'\}`pstar_2_RIin' & \{`pval_3_RIin'\}`pstar_3_RIin' & \{`pval_4_RIin'\}`pstar_4_RIin' \\ \\[-5pt]"		
	
	*local tex "`tex' Same = Different & [`pval_1_diff']`pstar_1_diff' & [`pval_2_diff']`pstar_2_diff' & [`pval_3_diff']`pstar_3_diff' & [`pval_4_diff']`pstar_4_diff' \\"
	*local tex "`tex' & \{`pval_1_RIdiff'\}`pstar_1_RIdiff' & \{`pval_2_RIdiff'\}`pstar_2_RIdiff' & \{`pval_3_RIdiff'\}`pstar_3_RIdiff' & \{`pval_4_RIdiff'\}`pstar_4_RIdiff' \\"				
	local tex "`tex' Observations & `obsHet1' & `obsHet2' & `obsHet3' & `obsHet4' \\ \hline" 
	local tex "`tex' \vspace{-8pt} & & & & \\"
	
	local tex "`tex' & \multicolumn{4}{c}{\textit{Panel B:} One Year Later (t+1)} \vspace{0.2em} \\"  

	local tex "`tex' Neighbor $\times$ Different `Hetero' & `coef_Lead1_out' & `coef_Lead2_out' & `coef_Lead3_out' & `coef_Lead4_out' \\"
	local tex "`tex' `bipart' & [`pval_Lead1_out']`pstar_Lead1_out' & [`pval_Lead2_out']`pstar_Lead2_out' & [`pval_Lead3_out']`pstar_Lead3_out' & [`pval_Lead4_out']`pstar_Lead4_out' \\"
	local tex "`tex' & \{`pval_Lead1_RIout'\}`pstar_Lead1_RIout' & \{`pval_Lead2_RIout'\}`pstar_Lead2_RIout' & \{`pval_Lead3_RIout'\}`pstar_Lead3_RIout' & \{`pval_Lead4_RIout'\}`pstar_Lead4_RIout' \\ \\[-5pt]"				
		
	local tex "`tex' Neighbor $\times$ Same `Hetero' & `coef_Lead1_in' & `coef_Lead2_in' & `coef_Lead3_in' & `coef_Lead4_in' \\"
	local tex "`tex'  & [`pval_Lead1_in']`pstar_Lead1_in' & [`pval_Lead2_in']`pstar_Lead2_in' & [`pval_Lead3_in']`pstar_Lead3_in' & [`pval_Lead4_in']`pstar_Lead4_in' \\"
	local tex "`tex' & \{`pval_Lead1_RIin'\}`pstar_Lead1_RIin' & \{`pval_Lead2_RIin'\}`pstar_Lead2_RIin' & \{`pval_Lead3_RIin'\}`pstar_Lead3_RIin' & \{`pval_Lead4_RIin'\}`pstar_Lead4_RIin' \\ \\[-5pt]"			
	
	*local tex "`tex' Same = Different & [`pval_Lead1_diff']`pstar_Lead1_diff' & [`pval_Lead2_diff']`pstar_Lead2_diff' & [`pval_Lead3_diff']`pstar_Lead3_diff' & [`pval_Lead4_diff']`pstar_Lead4_diff' \\"
	*local tex "`tex' & \{`pval_Lead1_RIdiff'\}`pstar_Lead1_RIdiff' & \{`pval_Lead2_RIdiff'\}`pstar_Lead2_RIdiff' & \{`pval_Lead3_RIdiff'\}`pstar_Lead3_RIdiff' & \{`pval_Lead4_RIdiff'\}`pstar_Lead4_RIdiff' \\"				
	local tex "`tex' Observations & `obsHetLead1' & `obsHetLead2' & `obsHetLead3' & `obsHetLead4' \\ \hline" 
	local tex "`tex' \vspace{-8pt} & & & & \\"
	
	local tex "`tex' & \multicolumn{4}{c}{\textit{Panel C:} Previous Year (Placebo) (t-1)} \vspace{0.2em} \\"  
	
	local tex "`tex' Neighbor $\times$ Different `Hetero' & `coef_Lag1_out' & `coef_Lag2_out' & `coef_Lag3_out' & `coef_Lag4_out' \\"
	local tex "`tex' `bipart' & [`pval_Lag1_out']`pstar_Lag1_out' & [`pval_Lag2_out']`pstar_Lag2_out' & [`pval_Lag3_out']`pstar_Lag3_out' & [`pval_Lag4_out']`pstar_Lag4_out' \\"
	local tex "`tex' & \{`pval_Lag1_RIout'\}`pstar_Lag1_RIout' & \{`pval_Lag2_RIout'\}`pstar_Lag2_RIout' & \{`pval_Lag3_RIout'\}`pstar_Lag3_RIout' & \{`pval_Lag4_RIout'\}`pstar_Lag4_RIout' \\ \\[-5pt]"				
		
	local tex "`tex' Neighbor $\times$ Same `Hetero' & `coef_Lag1_in' & `coef_Lag2_in' & `coef_Lag3_in' & `coef_Lag4_in' \\"
	local tex "`tex'  & [`pval_Lag1_in']`pstar_Lag1_in' & [`pval_Lag2_in']`pstar_Lag2_in' & [`pval_Lag3_in']`pstar_Lag3_in' & [`pval_Lag4_in']`pstar_Lag4_in' \\"
	local tex "`tex' & \{`pval_Lag1_RIin'\}`pstar_Lag1_RIin' & \{`pval_Lag2_RIin'\}`pstar_Lag2_RIin' & \{`pval_Lag3_RIin'\}`pstar_Lag3_RIin' & \{`pval_Lag4_RIin'\}`pstar_Lag4_RIin' \\ \\[-5pt]"			
	
	*local tex "`tex' Same = Different & [`pval_Lag1_diff']`pstar_Lag1_diff' & [`pval_Lag2_diff']`pstar_Lag2_diff' & [`pval_Lag3_diff']`pstar_Lag3_diff' & [`pval_Lag4_diff']`pstar_Lag4_diff' \\"
	*local tex "`tex' & \{`pval_Lag1_RIdiff'\}`pstar_Lag1_RIdiff' & \{`pval_Lag2_RIdiff'\}`pstar_Lag2_RIdiff' & \{`pval_Lag3_RIdiff'\}`pstar_Lag3_RIdiff' & \{`pval_Lag4_RIdiff'\}`pstar_Lag4_RIdiff' \\"				
	
	local tex "`tex' Observations & `obsHetLag1' & `obsHetLag2' & `obsHetLag3' & `obsHetLag4' \\ \hline" 	
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `mean3' & `mean4' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sd3' & `sd4' \\ \toprule" 
	local tex "`tex' \multicolumn{5}{p{15.4cm}}{\footnotesize \textit{Notes:} `eachPanelFour'" 
	local tex "`tex' `compliance' `yynnaa' `contested' `neighbor' `samehet'"
	local tex "`tex' `dyadSquare' `riCurly'"	
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `outcomeMeanA' `plab'}" 
	local tex "`tex' \\ \end{tabular} }"
	
	esttab Het1 Het2 Het3 Het4 using "`tablefolder'//pairVoteYesNoAbs`Hetero'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("Compliance" "Yes-Yes/No-No/Abstain-Abstain", pattern(1 1 0 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{All\\(1)}" "\specialcell{All\\(2)}" ///
	"\specialcell{Below 50th\\(3)}" "\specialcell{Below 25th\\(4)}")	
	
	timer off 24
	disp "DONE dyadYesNoAbs"
	timer list 24
}
if `mpCompound'==1 { // Do neighbour effects compound over time?

	timer on 28
	use "`votingfolder'/MP_session_analysis", clear

	// Define sessions that we will use
	g sampleSession = (!specialSession) & (!shortSession) & !(session_id == 149)
	g sampleSessionLag = !inlist(session_id,115,119,120,124,125,129,130,134,135,137,138,142,143,146,147,148,149)
	g sampleSessionLead = !inlist(session_id,118,119,123,124,128,129,133,134,136,137,141,142,145,146,147,148,149)
	g fracXfracLag = fracOther`Hetero'NeighborRI0*fracOth`Hetero'NeighborRI0Lag
	
	// Regressions
	local i=1
	foreach var of varlist non_compliance rebelWeak cospon_all ihs_cospon_all {
	
		areg `var' fracOther`Hetero'NeighborRI0 fracOth`Hetero'NeighborRI0Lag fracXfracLag if sampleSession, ///
		absorb(strataSessionPartyFE) cluster(MP_id)
		est sto MC`i'
		local obs`i' = e(N)	
		qui su `var' if e(sample)==1
		local mean`i' = string(r(mean),"%9.2g")
		
		local i=`i'+1
				
	}
	
	// Output Main Effects Table
	estout MC1 MC2 MC3 MC4 using "`tablefolder'//mpCompound`Hetero'A.tex", style(tex) replace ///
	keep(frac*) cells(b(star fmt(%9.2g)) se(par fmt(%9.2g))) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) ///		
	nolabel collabels(none) mlabels(none) ///
	varlabels(fracOther`Hetero'NeighborRI0 "Proportion Other-`Hetero' Neighbor" ///
			fracOth`Hetero'NeighborRI0Lag "Proportion Other-`Hetero' Neighbor (t-1)" ///
			fracXfracLag "Prop. Oth.-`Hetero' Neigh. $\times$ Prop. Oth.-`Hetero' Neigh. (t-1)")

	local tex "\input{mpCompound`Hetero'A} \vspace{-1.2em} \\ \\[-5pt] \hline"	
	local tex "`tex' Observations & `obs1' & `obs2' & `obs3' & `obs4' \\"
	local tex "`tex' Session $\times$ Party $\times$ Strata FE & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `mean3' & `mean4' \\ \toprule" 
	local tex "`tex' \multicolumn{5}{p{18.8cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `noncomplianceMP' `rebelWeakMP' `rawnumbMP' `ihsAll' `propothMP'"
	local tex "`tex' MP-clustered standard errors are in parentheses. `specShortDrop'" 
	local tex "`tex' Sessions are also dropped where lag would be a special/short session or a session in a different parliamentary term."
	local tex "`tex' `strataDefMP' `plab'}"	
	local tex "`tex' \\ \end{tabular} }"			
		
	esttab MC1 MC2 MC3 MC4 using "`tablefolder'//mpCompound`Hetero'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("\specialcell{Voting}" "\specialcell{Co-Sponsorship}", pattern(1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///	
	mtitles("\specialcell{Leader\\Non-\\Compliance\\(1)}" "\specialcell{\\Rebel\\Rate\\(2)}" ///
	"\specialcell{\\\\Number\\(3)}" "\specialcell{\\\\IHS\\(4)}")
	
	timer off 28
	disp "DONE mpVote"
	timer list 28
}
if `billsPassed'==1 { // Trends in legislative productivity

	timer on 29
	use "$path_pch/Data/raw/Co-Sponsorship/bills passed/bills_passed", clear
	
	// clean
	ren session_n session_id
	bys session_id: g Npassed = _N
	keep session session_id Npassed
	duplicates drop
	isid session_id
	
	// fix session_ids so that regular sessions have consecutive numbering
	g session_id_recode = session_id
	replace session_id_recode = session_id-1 if session_id>=120 & session_id<=123
	replace session_id_recode = session_id-2 if session_id>=125 & session_id<=128
	replace session_id_recode = session_id-3 if session_id>=130 & session_id<=133
	replace session_id_recode = session_id-4 if session_id>=135 & session_id<=136
	replace session_id_recode = session_id-5 if session_id>=138 & session_id<=141
	replace session_id_recode = session_id-6 if session_id>=143 & session_id<=146
	replace session_id_recode = session_id-7 if session_id==148
	
	// re-label sessions using new coding
	la de sessions2 115 "1991-92" 116 "1992-93" 117 "1993-94" 118 "1994-95" 119 "1995-96" 120 "1996-97" ///
		121 "1997-98" 122 "1998-99" 123 "1999-00" 124 "2000-01" 125 "2001-02" 126 "2002-03" ///
		127 "2003-04" 128 "2004-05" 129 "2005-06" 130 "2006-07" 131 "2007-08" 132 "2008-09" ///
		133 "2009-10" 134 "2010-11" 135 "2011-12" 136 "2012-13" 137 "2013-14" 138 "2014-15" 139 "2015-16" ///
		140 "2016-17" 141 "2017-18" 142 "2018-19"
	la val session_id_recode sessions2
	drop session_id		
		
	// plot leader non-compliance
	line Npassed session_id_recode, ///
		xlabel(115(2)141, angle(45) val noticks labsize(3.5)) ///
		ylabel(0(30)150, angle(horiontal) grid labsize(3.5)) ///
		ytitle("Number of Laws Passed", size(4.5)) ///
		xtitle("Session", size(4.5)) ///
		legend(off)
	graph export "`graphfolder'/trends/billsPassed.pdf", replace
	
	qui su Npassed
	local meanclean = round(r(mean),1)
	disp "Mean number of bills passed per regular session: `meanclean'"
	
	timer off 29
	disp "DONE billsPassed"
	timer list 29		
}
if `dyadSUTVA'==1 { // Dyadic voting results with front-back seating neighbor effect
	
	timer on 30
	forv r = 1/2 {	// Type of voting/cosponsorship measure?
		local measure: word `r' of "samevote4c" "voteSim4c"
			
			dis "`measure'"
			use `datamod', clear
							
			keep session_id samevote?c* votediff?c* voteSim?c* *RI* minMP maxMP pairStrata samecoalition sameparty ///
			samegender specialSession shortSession firstSession lastSession leadShortIncomplete	party_id party_id_d	///
			sessionXpairStrataXpartyPair *FB*
			
			/* Hetero: party ingroup and outgroup effect */
			
			// run regression
			areg `measure' neighborRI0Xsame`hetero' neighborRI0Xdiff`hetero' ///
							neighborFBRI0Xsame`hetero' neighborFBRI0Xdiff`hetero', absorb(sessionXpairStrataXpartyPair)
			est sto Het`r'
			local obsHet`r' = e(N)
			
			// prepare coefficient, standard error, p-value for export					
			local coef_`r'_in = string(_b[neighborRI0Xsame`hetero'],"%9.2g")
			local coef_`r'_out = string(_b[neighborRI0Xdiff`hetero'],"%9.2g")
			local coef_`r'_FBin = string(_b[neighborFBRI0Xsame`hetero'],"%9.2g")
			local coef_`r'_FBout = string(_b[neighborFBRI0Xdiff`hetero'],"%9.2g")				

			// randomization inference p-values
			if `RI'==1 {
				local Nbeta_extreme_in = 0
				local Nbeta_extreme_out = 0
				local Nbeta_extreme_FBin = 0
				local Nbeta_extreme_FBout = 0
				
				forv i=1/`runsRI' {
					areg `measure' neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero' ///
								neighborFBRI`i'Xsame`hetero' neighborFBRI`i'Xdiff`hetero', absorb(sessionXpairStrataXpartyPair)
								
					local absCoefIn = abs(_b[neighborRI`i'Xsame`hetero'])
					local absCoefOut = abs(_b[neighborRI`i'Xdiff`hetero'])
					local absCoefFBIn = abs(_b[neighborFBRI`i'Xsame`hetero'])
					local absCoefFBOut = abs(_b[neighborFBRI`i'Xdiff`hetero'])						
					
					local Nbeta_extreme_in = `Nbeta_extreme_in' + (`absCoefIn' >= abs(`coef_`r'_in')) // + 1 every time see more extreme beta
					local Nbeta_extreme_out = `Nbeta_extreme_out' + (`absCoefOut' >= abs(`coef_`r'_out')) 
					local Nbeta_extreme_FBin = `Nbeta_extreme_FBin' + (`absCoefFBIn' >= abs(`coef_`r'_FBin'))
					local Nbeta_extreme_FBout = `Nbeta_extreme_FBout' + (`absCoefFBOut' >= abs(`coef_`r'_FBout')) 						
				}
				
				local pval_`r'_RIin = string(`Nbeta_extreme_in'/`runsRI',"%9.2g")
				local pval_`r'_RIout = string(`Nbeta_extreme_out'/`runsRI',"%9.2g")
				local pval_`r'_FBRIin = string(`Nbeta_extreme_FBin'/`runsRI',"%9.2g")
				local pval_`r'_FBRIout = string(`Nbeta_extreme_FBout'/`runsRI',"%9.2g")					
			}
			else {
				local pval_`r'_RIin = 999
				local pval_`r'_RIout = 999
				local pval_`r'_FBRIin = 999
				local pval_`r'_FBRIout = 999					
			}					
			
			// get stars for all pvals
			foreach y in RIin RIout FBRIin FBRIout {
				local pstar_`r'_`y' = "" // reset p-value
				if `pval_`r'_`y''<0.10 & `pval_`r'_`y''>=0.05 local pstar_`r'_`y' = "*"
				if `pval_`r'_`y''<0.05 & `pval_`r'_`y''>=0.01 local pstar_`r'_`y' = "**"
				if `pval_`r'_`y''<0.01 local pstar_`r'_`y' = "***"
			}				
	}
		
	// Output Heterogeneous Effects Table
	local tex "Neighbor $\times$ Different `Hetero' & `coef_1_out' & `coef_2_out' \\"
	local tex "`tex' & \{`pval_1_RIout'\}`pstar_1_RIout' & \{`pval_2_RIout'\}`pstar_2_RIout' \\ \\[-5pt]"		
	
	local tex "`tex' Neighbor $\times$ Same `Hetero' & `coef_1_in' & `coef_2_in' \\"
	local tex "`tex' & \{`pval_1_RIin'\}`pstar_1_RIin' & \{`pval_2_RIin'\}`pstar_2_RIin' \\ \\[-5pt]"		
	
	local tex "`tex' Front-Back Neighbor $\times$ Different `Hetero' & `coef_1_FBout' & `coef_2_FBout' \\"
	local tex "`tex' & \{`pval_1_FBRIout'\}`pstar_1_FBRIout' & \{`pval_2_FBRIout'\}`pstar_2_FBRIout' \\ \\[-5pt]"		
	
	local tex "`tex' Front-Back Neighbor $\times$ Same `Hetero' & `coef_1_FBin' & `coef_2_FBin' \\"
	local tex "`tex' & \{`pval_1_FBRIin'\}`pstar_1_FBRIin' & \{`pval_2_FBRIin'\}`pstar_2_FBRIin' \\ \\[-5pt] \hline"	

	local tex "`tex' Observations & `obsHet1' & `obsHet2' \\" 
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y \\ \toprule" 
	
	local tex "`tex' \multicolumn{3}{p{11.5cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `compliance' `similarity'"
	local tex "`tex' `neighbor' Front-Back Neighbor is a dummy variable equal to one if the MPs in the pair are sitting immediately in front of or behind each other. `samehet'"
	local tex "`tex' `riCurly'"
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `plab'}"
	local tex "`tex' \\ \end{tabular} }"
	
	esttab Het1 Het2 using "`tablefolder'//pairVoteSUTVA`Hetero'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("\specialcell{Contemporaneous Effect (t)}", pattern(1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Compliance\\(1)}" "\specialcell{Similarity\\(2)}")	
	
	timer off 30
	disp "DONE dyadSUTVA"
	timer list 30
}
if `billType'==1 { // Dyadic voting results by bill type
	
	timer on 31
	
	// store regular sessions
	use "`votingfolder'/MP_session_analysis", clear
	g sampleSession = (!specialSession) & (!shortSession) & !(session_id == 149)
	keep if sampleSession==1
	keep session_id
	duplicates drop
	save `regSess2'
	
	// store percentage of each vote
	insheet using "`votingfolderraw'/vote_info_20210626.csv", comma clear
	isid vote_id
	assert session_id==session_id_alt
	drop session_id_alt datetime bill_id
	merge m:1 session_id using `regSess2', assert(1 3) keep(3) nogen
	keep vote_id billtype
	
	local i=1
	foreach x in Brtt. Frv. Till. Þskj. {
		g type`i' = billtype=="`x'"
		local i=`i'+1
	}
	g type5 = type1+type2+type3+type4==0
	
	qui su type1
	local brtPerc = string(round(r(mean)*100,1))
	qui su type2
	local frvPerc = string(round(r(mean)*100,1))
	qui su type3
	local tilPerc = string(round(r(mean)*100,1))
	qui su type4
	local pskPerc = string(round(r(mean)*100,1))
	qui su type5
	local othPerc = string(round(r(mean)*100,1))

	forv r = 1/5 {	// Type of voting/cosponsorship measure?
		local measure: word `r' of "voteSim4c_frv" "voteSim4c_brt" "voteSim4c_psk" "voteSim4c_til" "voteSim4c_rest"
			
			dis "`measure'"
			use `datamod', clear
			keep session_id voteSim?c* *RI* minMP maxMP ///
			pairStrata samecoalition sameparty ///
			samegender specialSession shortSession firstSession lastSession leadShortIncomplete	party_id party_id_d	///
			sessionXpairStrataXpartyPair
			
			/* Neighborhood effect */
			
			// residualize
			qui areg `measure' if !mi(`measure') & !mi(neighborRI0) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
			local numN = e(N)
			local numDummy = e(df_a) 
			predict yresid, residuals
			qui areg neighborRI0 if !mi(`measure') & !mi(neighborRI0) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
			local numN2 = e(N)
			predict xresid, residuals
			assert `numN' == `numN2'
			
			// run regression
			xi: regdyad2 yresid xresid if !mi(`measure') & !mi(neighborRI0) & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d), ///
				dyads(minMP maxMP)
			est sto M`r'
			local obs`r' = e(N)
			qui su `measure' if e(sample)==1
			local mean`r' = string(r(mean),"%9.2g")
			local sd`r' = string(r(sd),"%9.2g")
			local sd`r'raw = `r(sd)'
			local numReg = 1
			
			// prepare coefficient, standard error, p-value for export
			local coef_`r'_av = _b[xresid]
			local se_`r'_av = string(_se[xresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1)),"%9.2g")
			local t_df = `numN'-`numReg'-`numDummy'-1
			local pval_`r'_av = string(ttail(`t_df',abs(`coef_`r'_av'/`se_`r'_av'))*2,"%9.2g")					
			drop yresid xresid	
			
			// randomization inference p-values
			if `RI'==1 {
				local Nbeta_extreme = 0
				forv i=1/`runsRI' {
					areg `measure' neighborRI`i', absorb(sessionXpairStrataXpartyPair)
					local absCoef = abs(_b[neighborRI`i'])
					local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`r'_av')) // + 1 every time see more extreme beta
				}
				local pval_`r'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
			}
			else {
				local pval_`r'_RI = 999
			}				
			
			/* Hetero: party ingroup and outgroup effect */
			
			// residualize			
			qui areg `measure' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair)
			local numN = e(N)
			local numDummy = e(df_a) 
			predict yresid, residuals
			
			qui areg neighborRI0Xsame`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
			local numN2 = e(N)
			predict xinresid, residuals
			assert `numN' == `numN2'
			qui areg neighborRI0Xdiff`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
			predict xoutresid, residuals
			
			// run regression
			xi: regdyad2 yresid xinresid xoutresid if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') ///
				& !mi(pairStrata) & !mi(party_id) & !mi(party_id_d), dyads(minMP maxMP)
			est sto Het`r'
			local obsHet`r' = e(N)
			local numReg = 2
			matrix varMatrix = e(V)
			
			// prepare coefficient, standard error, p-value for export					
			local coef_`r'_in = string(_b[xinresid],"%9.2g")
			local coef_`r'_out = string(_b[xoutresid],"%9.2g")
			local se_`r'_in = _se[xinresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
			local se_`r'_out = _se[xoutresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
			local t_df = `numN'-`numReg'-`numDummy'-1
			local pval_`r'_in =  string(ttail(`t_df',abs(`coef_`r'_in'/`se_`r'_in'))*2,"%9.2g")
			local pval_`r'_out =  string(ttail(`t_df',abs(`coef_`r'_out'/`se_`r'_out'))*2,"%9.2g")
			
			// pval calculation for the difference
			local diff = `coef_`r'_in' - `coef_`r'_out'
			local se_`r'_diff = sqrt(varMatrix[1,1]+varMatrix[2,2]-2*varMatrix[2,1])
			local pval_`r'_diff =  string(ttail(`t_df',abs(`diff'/`se_`r'_diff'))*2,"%9.2g")
			drop yresid xinresid xoutresid
			
			// randomization inference p-values
			if `RI'==1 {
				local Nbeta_extreme_in = 0
				local Nbeta_extreme_out = 0
				local Nbeta_extreme_diff = 0
				forv i=1/`runsRI' {
					areg `measure' neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero', absorb(sessionXpairStrataXpartyPair)
					local absCoefIn = abs(_b[neighborRI`i'Xsame`hetero'])
					local absCoefOut = abs(_b[neighborRI`i'Xdiff`hetero'])
					local Nbeta_extreme_in = `Nbeta_extreme_in' + (`absCoefIn' >= abs(`coef_`r'_in')) // + 1 every time see more extreme beta
					local Nbeta_extreme_out = `Nbeta_extreme_out' + (`absCoefOut' >= abs(`coef_`r'_out')) 
				
					// for difference between in vs out p-value we need separate regressions, using "modified" outcome
					g `measure'_m = `measure'
					replace `measure'_m = `measure'_m + `coef_`r'_av' if neighborRI`i' == 1 & neighborRI0 == 0
					replace `measure'_m = `measure'_m - `coef_`r'_av' if neighborRI`i' == 0 & neighborRI0 == 1	
					
					// run regression with modified outcome
					areg `measure'_m neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero', absorb(sessionXpairStrataXpartyPair)
					local absCoefDiff = abs(_b[neighborRI`i'Xsame`hetero']-_b[neighborRI`i'Xdiff`hetero'])
					local Nbeta_extreme_diff = `Nbeta_extreme_diff' + (`absCoefDiff' >= abs(`diff')) 
					drop `measure'_m
				}
				local pval_`r'_RIin = string(`Nbeta_extreme_in'/`runsRI',"%9.2g")
				local pval_`r'_RIout = string(`Nbeta_extreme_out'/`runsRI',"%9.2g")	
				local pval_`r'_RIdiff = string(`Nbeta_extreme_diff'/`runsRI',"%9.2g")	
			}
			else {
				local pval_`r'_RIin = 999
				local pval_`r'_RIout = 999	
				local pval_`r'_RIdiff = 999										
			}					
			
			// get stars for all pvals
			foreach y in av in out diff RI RIin RIout RIdiff {
				local pstar_`r'_`y' = "" // reset p-value
				if `pval_`r'_`y''<0.10 & `pval_`r'_`y''>=0.05 local pstar_`r'_`y' = "*"
				if `pval_`r'_`y''<0.05 & `pval_`r'_`y''>=0.01 local pstar_`r'_`y' = "**"
				if `pval_`r'_`y''<0.01 local pstar_`r'_`y' = "***"
			}
	
			// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
			foreach y in av in out diff {
				if `pval_`r'_`y''<0.001 local pval_`r'_`y' = "<0.001" 
			}						
	}
			
	// Output Heterogeneous Effects Table
	local tex "Neighbor $\times$ Different `Hetero' & `coef_1_out' & `coef_2_out' & `coef_3_out' & `coef_4_out' & `coef_5_out' \\"
	local tex "`tex' `bipart' & [`pval_1_out']`pstar_1_out' & [`pval_2_out']`pstar_2_out' & [`pval_3_out']`pstar_3_out' & [`pval_4_out']`pstar_4_out' & [`pval_5_out']`pstar_5_out' \\"
	local tex "`tex' & \{`pval_1_RIout'\}`pstar_1_RIout' & \{`pval_2_RIout'\}`pstar_2_RIout' & \{`pval_3_RIout'\}`pstar_3_RIout' & \{`pval_4_RIout'\}`pstar_4_RIout' & \{`pval_5_RIout'\}`pstar_5_RIout' \\ \\[-5pt]"		
	
	local tex "`tex' Neighbor $\times$ Same `Hetero' & `coef_1_in' & `coef_2_in' & `coef_3_in' & `coef_4_in'  & `coef_5_in' \\"
	local tex "`tex'  & [`pval_1_in']`pstar_1_in' & [`pval_2_in']`pstar_2_in' & [`pval_3_in']`pstar_3_in' & [`pval_4_in']`pstar_4_in' & [`pval_5_in']`pstar_5_in' \\"
	local tex "`tex' & \{`pval_1_RIin'\}`pstar_1_RIin' & \{`pval_2_RIin'\}`pstar_2_RIin' & \{`pval_3_RIin'\}`pstar_3_RIin' & \{`pval_4_RIin'\}`pstar_4_RIin' & \{`pval_5_RIin'\}`pstar_5_RIin' \\ \\[-5pt]"		
				
	local tex "`tex' Same = Different & [`pval_1_diff']`pstar_1_diff' & [`pval_2_diff']`pstar_2_diff' & [`pval_3_diff']`pstar_3_diff' & [`pval_4_diff']`pstar_4_diff' & [`pval_5_diff']`pstar_5_diff' \\"
	local tex "`tex' & \{`pval_1_RIdiff'\}`pstar_1_RIdiff' & \{`pval_2_RIdiff'\}`pstar_2_RIdiff' & \{`pval_3_RIdiff'\}`pstar_3_RIdiff' & \{`pval_4_RIdiff'\}`pstar_4_RIdiff' & \{`pval_5_RIdiff'\}`pstar_5_RIdiff' \\ \\[-5pt] \hline"				
	
	local tex "`tex' Observations & `obsHet1' & `obsHet2' & `obsHet3' & `obsHet4' & `obsHet5' \\" 
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `mean3' & `mean4' & `mean5' \\"  
	local tex "`tex' Percentage of All Votes & `frvPerc' & `brtPerc' & `pskPerc' & `tilPerc' & `othPerc' \\ \toprule" 
	
	local tex "`tex' \multicolumn{6}{p{17cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `similarity'"
	local tex "`tex' The results are shown separately for voting on the four most common vote categories, plus a residual category."
	local tex "`tex' The four main categories are: draft bills, amendments, resolutions or parliamentary resolutions, and parliamentary documents."
	local tex "`tex' `neighbor' `samehet'"
	local tex "`tex' `dyadSquare' `riCurly'"
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `plab'}"
	local tex "`tex' \\ \end{tabular} }"
	
	esttab Het1 Het2 Het3 Het4 Het5 using "`tablefolder'//billType`Hetero'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("Contemporaneous Effect (t) on Similarity", pattern(1 0 0 0 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Bill\\(1)}" "\specialcell{Amendment\\(2)}" ///
	"\specialcell{Document\\(3)}" "\specialcell{Resolution\\(4)}" ///
	"\specialcell{Other\\(5)}")
	
	// frv = frumvarp = draft bill/legislation
	// brt = amendment
	// til = tillaga = resolution or parliamentary resolution
	// psk = þingskjal = parliamentary document
	
	timer off 31
	disp "DONE billType"
	timer list 31
}
if `mpVoteExper'==1 { // Weaker effects for experienced "cue-givers"?

	timer on 32
	use "`votingfolder'/MP_session_analysis", clear

	// Define sessions that we will use
	g sampleSession = (!specialSession) & (!shortSession) & !(session_id == 149)
	g sampleSessionLag = !inlist(session_id,115,119,120,124,125,129,130,134,135,137,138,142,143,146,147,148,149)
	g sampleSessionLead = !inlist(session_id,118,119,123,124,128,129,133,134,136,137,141,142,145,146,147,148,149)
	
	// Regressions
	forv k = 1/3 { 		// Time period?
		local time: word `k' of "Lag" "" "Lead"	
	
		forv r = 1/2 {	// Type of voting/cosponsorship measure?
			local measure: word `r' of "non_compliance" "rebelWeak"	

			areg `measure'`time' fracOther`Hetero'NeighborRI0 fracOther`Hetero'RI0XE sessExper if sampleSession`time', ///
					absorb(strataSessionPartyFE) cluster(MP_id)
			est sto M`time'`r'
			local obs`time'`r' = e(N)	
			qui su `measure'`time' if e(sample)==1
			local mean`time'`r' = string(r(mean),"%9.2g")
		}
	}
	
	// Output Main Effects Table
	estout M1 M2 MLead1 MLead2 MLag1 MLag2 using "`tablefolder'//mpVoteExper`Hetero'A.tex", style(tex) replace ///
	keep(frac* sessExper) cells(b(star fmt(%9.2g)) se(par fmt(%9.2g))) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) ///		
	nolabel collabels(none) mlabels(none) ///
	varlabels(fracOther`Hetero'NeighborRI0 "Proportion Other-`Hetero' Neighbor" ///
			fracOther`Hetero'RI0XE "Proportion Other-`Hetero' Neighbor $\times$ Experience" ///
			sessExper "Experience")

	local tex "\input{mpVoteExper`Hetero'A} \vspace{-1.2em} \\ \\[-5pt] \hline"	
	local tex "`tex' Observations & `obs1' & `obs2' & `obsLead1' & `obsLead2' & `obsLag1' & `obsLag2' \\"
	local tex "`tex' Session $\times$ Party $\times$ Strata FE & Y & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `meanLead1' & `meanLead2' & `meanLag1' & `meanLag2' \\ \toprule" 
	local tex "`tex' \multicolumn{7}{p{21.2cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `noncomplianceMP' `rebelWeakMP' `propothMP'"
	local tex "`tex' Experience is the number of sessions since first session as Althingi member."
	local tex "`tex' MP-clustered standard errors are in parentheses."
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDefMP' `plab'}"	
	local tex "`tex' \\ \end{tabular} }"			
		
	esttab M1 M2 MLead1 MLead2 MLag1 MLag2 using "`tablefolder'//mpVoteExper`Hetero'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("\specialcell{Contemporaneous\\Effect (t)}" "\specialcell{One Year\\Later (t+1)}" ///
			"\specialcell{Previous Year\\(Placebo) (t-1)}", pattern(1 0 1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Leader\\Non-\\Compliance\\(1)}" "\specialcell{\\Rebel\\Rate\\(2)}" ///
	"\specialcell{Leader\\Non-\\Compliance\\(3)}" "\specialcell{\\Rebel\\Rate\\(4)}" ///	
	"\specialcell{Leader\\Non-\\Compliance\\(5)}" "\specialcell{\\Rebel\\Rate\\(6)}")
	
	timer off 32
	disp "DONE mpVoteExper"
	timer list 32
}
if `dyadVotePartyDist'==1 { // Dyadic voting neighbour effects by ideological distance between parties 
	
	timer on 33
	forv k = 1/3 { 		// Time period?
		local time: word `k' of "Lag" "" "Lead"

		forv r = 1/2 {	// Type of voting/cosponsorship measure?
			local measure: word `r' of "samevote4c" "voteSim4c"
				
				dis "`measure'`time'"
				use `datamod', clear
								
				keep session_id samevote?c* votediff?c* voteSim?c* *RI* minMP maxMP pairStrata samecoalition sameparty ///
				samegender specialSession shortSession firstSession lastSession leadShortIncomplete	party_id party_id_d	///
				sessionXpairStrataXpartyPair left_rightDiff partyClose partyFar partyVClose partyMid partyVFar
				
				if `k'==1 {
					drop if firstSession==1 
				}
				else if `k'==3 {
					drop if lastSession==1
					drop if leadShortIncomplete==1
				}
				
				keep if sameparty==0
				
				/* Calculate weights */
				if `reweight'==1 {
					bys sessionXpairStrataXpartyPair: egen probTreat = mean(neighborRI0)
					g invProb = 1/probTreat if neighborRI0==1
					replace invProb = 1/(1-probTreat) if neighborRI0==0
				}	
				
				/* Neighborhood effect */
				
				// residualize
				qui areg `measure'`time' if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				local numN = e(N)
				local numDummy = e(df_a) 
				predict yresid, residuals
				qui areg neighborRI0 if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				local numN2 = e(N)
				predict xresid, residuals
				assert `numN' == `numN2'
				
				// run regression
				xi: regdyad2 yresid xresid if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d) `wgt', ///
					dyads(minMP maxMP)
				est sto M`time'`r'
				local obs`time'`r' = e(N)
				qui su `measure'`time' if e(sample)==1
				local mean`time'`r' = string(r(mean),"%9.2g")
				local sd`time'`r' = string(r(sd),"%9.2g")
				local sd`time'`r'raw = `r(sd)'
				local numReg = 1
				
				// prepare coefficient, standard error, p-value for export
				local coef_`time'`r'_av = _b[xresid]
				local se_`time'`r'_av = string(_se[xresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1)),"%9.2g")
				local t_df = `numN'-`numReg'-`numDummy'-1
				local pval_`time'`r'_av = string(ttail(`t_df',abs(`coef_`time'`r'_av'/`se_`time'`r'_av'))*2,"%9.2g")					
				drop yresid xresid	
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme = 0
					forv i=1/`runsRI' {
						areg `measure'`time' neighborRI`i', absorb(sessionXpairStrataXpartyPair)
						local absCoef = abs(_b[neighborRI`i'])
						local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`time'`r'_av')) // + 1 every time see more extreme beta
					}
					local pval_`time'`r'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
				}
				else {
					local pval_`time'`r'_RI = 999
				}					
				
				/* Hetero: close vs. distant diff-parties */
				
				// residualize			
				qui areg `measure'`time' if !mi(neighborRI0XpartyClose) & !mi(neighborRI0XpartyFar) ///
					& !mi(`measure'`time') & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair)
				local numN = e(N)
				local numDummy = e(df_a) 
				predict yresid, residuals
				
				qui areg neighborRI0XpartyClose if !mi(neighborRI0XpartyClose) & !mi(neighborRI0XpartyFar) ///
					& !mi(`measure'`time') & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				local numN2 = e(N)
				predict xinresid, residuals
				assert `numN' == `numN2'
				qui areg neighborRI0XpartyFar if !mi(neighborRI0XpartyClose) & !mi(neighborRI0XpartyFar) ///
					& !mi(`measure'`time') & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				predict xoutresid, residuals
				
				// run regression
				xi: regdyad2 yresid xinresid xoutresid if !mi(neighborRI0XpartyClose) & !mi(neighborRI0XpartyFar) ///
					& !mi(`measure'`time') & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d) `wgt', dyads(minMP maxMP)
				est sto Het`time'`r'
				local obsHet`time'`r' = e(N)
				local numReg = 2
				matrix varMatrix = e(V)
				
				// prepare coefficient, standard error, p-value for export					
				local coef_`time'`r'_in = string(_b[xinresid],"%9.2g")
				local coef_`time'`r'_out = string(_b[xoutresid],"%9.2g")
				local se_`time'`r'_in = _se[xinresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
				local se_`time'`r'_out = _se[xoutresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
				local t_df = `numN'-`numReg'-`numDummy'-1
				local pval_`time'`r'_in =  string(ttail(`t_df',abs(`coef_`time'`r'_in'/`se_`time'`r'_in'))*2,"%9.2g")
				local pval_`time'`r'_out =  string(ttail(`t_df',abs(`coef_`time'`r'_out'/`se_`time'`r'_out'))*2,"%9.2g")
				
				// pval calculation for the difference
				local diff = `coef_`time'`r'_in' - `coef_`time'`r'_out'
				local se_`time'`r'_diff = sqrt(varMatrix[1,1]+varMatrix[2,2]-2*varMatrix[2,1])
				local pval_`time'`r'_diff =  string(ttail(`t_df',abs(`diff'/`se_`time'`r'_diff'))*2,"%9.2g")
				drop yresid xinresid xoutresid
				
				// diff-party point estimate and 95% confidence interval for figure
				if `k'==2 {
					local core_`r'_out = `coef_`r'_out'/`sd`r'raw'
					local corel_`r'_out = (`coef_`r'_out' - invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
					local coreu_`r'_out = (`coef_`r'_out' + invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
				}
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme_in = 0
					local Nbeta_extreme_out = 0
					local Nbeta_extreme_diff = 0
					forv i=1/`runsRI' {
						areg `measure'`time' neighborRI`i'XpartyClose neighborRI`i'XpartyFar, absorb(sessionXpairStrataXpartyPair)
						local absCoefIn = abs(_b[neighborRI`i'XpartyClose])
						local absCoefOut = abs(_b[neighborRI`i'XpartyFar])
						local Nbeta_extreme_in = `Nbeta_extreme_in' + (`absCoefIn' >= abs(`coef_`time'`r'_in')) // + 1 every time see more extreme beta
						local Nbeta_extreme_out = `Nbeta_extreme_out' + (`absCoefOut' >= abs(`coef_`time'`r'_out')) 
					
						// for difference between in vs out p-value we need separate regressions, using "modified" outcome
						g `measure'`time'_m = `measure'`time'
						replace `measure'`time'_m = `measure'`time'_m + `coef_`time'`r'_av' if neighborRI`i' == 1 & neighborRI0 == 0
						replace `measure'`time'_m = `measure'`time'_m - `coef_`time'`r'_av' if neighborRI`i' == 0 & neighborRI0 == 1	
						
						// run regression with modified outcome
						areg `measure'`time'_m neighborRI`i'XpartyClose neighborRI`i'XpartyFar, absorb(sessionXpairStrataXpartyPair)
						local absCoefDiff = abs(_b[neighborRI`i'XpartyClose]-_b[neighborRI`i'XpartyFar])
						local Nbeta_extreme_diff = `Nbeta_extreme_diff' + (`absCoefDiff' >= abs(`diff')) 
						drop `measure'`time'_m
					}
					local pval_`time'`r'_RIin = string(`Nbeta_extreme_in'/`runsRI',"%9.2g")
					local pval_`time'`r'_RIout = string(`Nbeta_extreme_out'/`runsRI',"%9.2g")	
					local pval_`time'`r'_RIdiff = string(`Nbeta_extreme_diff'/`runsRI',"%9.2g")	
				}
				else {
					local pval_`time'`r'_RIin = 999
					local pval_`time'`r'_RIout = 999	
					local pval_`time'`r'_RIdiff = 999										
				}					
				
				// get stars for all pvals
				foreach y in av in out diff RI RIin RIout RIdiff {
					local pstar_`time'`r'_`y' = "" // reset p-value
					if `pval_`time'`r'_`y''<0.10 & `pval_`time'`r'_`y''>=0.05 local pstar_`time'`r'_`y' = "*"
					if `pval_`time'`r'_`y''<0.05 & `pval_`time'`r'_`y''>=0.01 local pstar_`time'`r'_`y' = "**"
					if `pval_`time'`r'_`y''<0.01 local pstar_`time'`r'_`y' = "***"
				}
				
				// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
				foreach y in av in out diff {
					if `pval_`time'`r'_`y''<0.001 local pval_`time'`r'_`y' = "<0.001" 
				}					
		}
	}
		
	// Output Heterogeneous Effects Table
	local tex "Neighbor $\times$ Distant Other-Party & `coef_1_out' & `coef_2_out' & `coef_Lead1_out' & `coef_Lead2_out' & `coef_Lag1_out' & `coef_Lag2_out' \\"
	local tex "`tex' & [`pval_1_out']`pstar_1_out' & [`pval_2_out']`pstar_2_out' & [`pval_Lead1_out']`pstar_Lead1_out' & [`pval_Lead2_out']`pstar_Lead2_out' & [`pval_Lag1_out']`pstar_Lag1_out' & [`pval_Lag2_out']`pstar_Lag2_out' \\"
	local tex "`tex' & \{`pval_1_RIout'\}`pstar_1_RIout' & \{`pval_2_RIout'\}`pstar_2_RIout' & \{`pval_Lead1_RIout'\}`pstar_Lead1_RIout' & \{`pval_Lead2_RIout'\}`pstar_Lead2_RIout' & \{`pval_Lag1_RIout'\}`pstar_Lag1_RIout' & \{`pval_Lag2_RIout'\}`pstar_Lag2_RIout' \\ \\[-5pt]"		
	
	local tex "`tex' Neighbor $\times$ Close Other-Party & `coef_1_in' & `coef_2_in' & `coef_Lead1_in' & `coef_Lead2_in'  & `coef_Lag1_in' & `coef_Lag2_in' \\"
	local tex "`tex'  & [`pval_1_in']`pstar_1_in' & [`pval_2_in']`pstar_2_in' & [`pval_Lead1_in']`pstar_Lead1_in' & [`pval_Lead2_in']`pstar_Lead2_in' & [`pval_Lag1_in']`pstar_Lag1_in' & [`pval_Lag2_in']`pstar_Lag2_in' \\"
	local tex "`tex' & \{`pval_1_RIin'\}`pstar_1_RIin' & \{`pval_2_RIin'\}`pstar_2_RIin' & \{`pval_Lead1_RIin'\}`pstar_Lead1_RIin' & \{`pval_Lead2_RIin'\}`pstar_Lead2_RIin' & \{`pval_Lag1_RIin'\}`pstar_Lag1_RIin' & \{`pval_Lag2_RIin'\}`pstar_Lag2_RIin' \\ \\[-5pt]"		
		
	local tex "`tex' Close = Distant & [`pval_1_diff']`pstar_1_diff' & [`pval_2_diff']`pstar_2_diff' & [`pval_Lead1_diff']`pstar_Lead1_diff' & [`pval_Lead2_diff']`pstar_Lead2_diff' & [`pval_Lag1_diff']`pstar_Lag1_diff' & [`pval_Lag2_diff']`pstar_Lag2_diff' \\"
	local tex "`tex' & \{`pval_1_RIdiff'\}`pstar_1_RIdiff' & \{`pval_2_RIdiff'\}`pstar_2_RIdiff' & \{`pval_Lead1_RIdiff'\}`pstar_Lead1_RIdiff' & \{`pval_Lead2_RIdiff'\}`pstar_Lead2_RIdiff' & \{`pval_Lag1_RIdiff'\}`pstar_Lag1_RIdiff' & \{`pval_Lag2_RIdiff'\}`pstar_Lag2_RIdiff' \\ \\[-5pt] \hline"				
	
	local tex "`tex' Observations & `obsHet1' & `obsHet2' & `obsHetLead1' & `obsHetLead2' & `obsHetLag1' & `obsHetLag2' \\" 
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `meanLead1' & `meanLead2' & `meanLag1' & `meanLag2' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sdLead1' & `sdLead2' & `sdLag1' & `sdLag2' \\ \toprule" 
	
	local tex "`tex' \multicolumn{7}{p{20cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' Sample includes only different-party pairs. `compliance' `similarity'"
	local tex "`tex' `neighbor'"
	local tex "`tex' Distant Other-Party is a dummy variable equal to one if the difference between the left-right score (from ParlGov) of the two parties in a pair is above-median."
	local tex "`tex' Close Other-Party is a dummy variable equal to one if the difference is below-median."
	local tex "`tex' `dyadSquare' `riCurly'"
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `plab'}"
	local tex "`tex' \\ \end{tabular} }"
	
	esttab Het1 Het2 HetLead1 HetLead2 HetLag1 HetLag2 using "`tablefolder'//pairVotePartyDist.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("\specialcell{Contemporaneous\\Effect (t)}" "\specialcell{One Year\\Later (t+1)}" ///
			"\specialcell{Previous Year\\(Placebo) (t-1)}", pattern(1 0 1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Compliance\\(1)}" "\specialcell{Similarity\\(2)}" ///
	"\specialcell{Compliance\\(3)}" "\specialcell{Similarity\\(4)}" ///	
	"\specialcell{Compliance\\(5)}" "\specialcell{Similarity\\(6)}")		
				
	timer off 33
	disp "DONE dyadVotePartyDist"
	timer list 33				
}
if `dyadVotePartyTerc'==1 { // Dyadic voting neighbour effects by ideological distance between parties 
	
	timer on 34
	forv k = 1/3 { 		// Time period?
		local time: word `k' of "Lag" "" "Lead"

		forv r = 1/2 {	// Type of voting/cosponsorship measure?
			local measure: word `r' of "samevote4c" "voteSim4c"
				
				dis "`measure'`time'"
				use `datamod', clear
								
				keep session_id samevote?c* votediff?c* voteSim?c* *RI* minMP maxMP pairStrata samecoalition sameparty ///
				samegender specialSession shortSession firstSession lastSession leadShortIncomplete	party_id party_id_d	///
				sessionXpairStrataXpartyPair left_rightDiff partyClose partyFar partyVClose partyMid partyVFar
				
				if `k'==1 {
					drop if firstSession==1 
				}
				else if `k'==3 {
					drop if lastSession==1
					drop if leadShortIncomplete==1
				}
				
				keep if sameparty==0
				
				/* Calculate weights */
				if `reweight'==1 {
					bys sessionXpairStrataXpartyPair: egen probTreat = mean(neighborRI0)
					g invProb = 1/probTreat if neighborRI0==1
					replace invProb = 1/(1-probTreat) if neighborRI0==0
				}	
				
				/* Neighborhood effect */
				
				// residualize
				qui areg `measure'`time' if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				local numN = e(N)
				local numDummy = e(df_a) 
				predict yresid, residuals
				qui areg neighborRI0 if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				local numN2 = e(N)
				predict xresid, residuals
				assert `numN' == `numN2'
				
				// run regression
				xi: regdyad2 yresid xresid if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d) `wgt', ///
					dyads(minMP maxMP)
				est sto M`time'`r'
				local obs`time'`r' = e(N)
				qui su `measure'`time' if e(sample)==1
				local mean`time'`r' = string(r(mean),"%9.2g")
				local sd`time'`r' = string(r(sd),"%9.2g")
				local sd`time'`r'raw = `r(sd)'
				local numReg = 1
				
				// prepare coefficient, standard error, p-value for export
				local coef_`time'`r'_av = _b[xresid]
				local se_`time'`r'_av = string(_se[xresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1)),"%9.2g")
				local t_df = `numN'-`numReg'-`numDummy'-1
				local pval_`time'`r'_av = string(ttail(`t_df',abs(`coef_`time'`r'_av'/`se_`time'`r'_av'))*2,"%9.2g")					
				drop yresid xresid	
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme = 0
					forv i=1/`runsRI' {
						areg `measure'`time' neighborRI`i', absorb(sessionXpairStrataXpartyPair)
						local absCoef = abs(_b[neighborRI`i'])
						local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`time'`r'_av')) // + 1 every time see more extreme beta
					}
					local pval_`time'`r'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
				}
				else {
					local pval_`time'`r'_RI = 999
				}					
				
				/* Hetero: v. close vs. medium vs. distant diff-parties */
				
				// residualize			
				qui areg `measure'`time' if !mi(neighborRI0XpartyVClose) ///
					& !mi(`measure'`time') & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair)
				local numN = e(N)
				local numDummy = e(df_a) 
				predict yresid, residuals
				
				qui areg neighborRI0XpartyVClose if !mi(neighborRI0XpartyVClose) ///
					& !mi(`measure'`time') & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				local numN2 = e(N)
				predict xinresid, residuals
				assert `numN' == `numN2'
				
				qui areg neighborRI0XpartyMid if !mi(neighborRI0XpartyVClose) ///
					& !mi(`measure'`time') & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				predict xmidresid, residuals
				
				qui areg neighborRI0XpartyVFar if !mi(neighborRI0XpartyVClose) ///
					& !mi(`measure'`time') & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				predict xoutresid, residuals				
				
				// run regression
				xi: regdyad2 yresid xinresid xmidresid xoutresid if !mi(neighborRI0XpartyVClose) ///
					& !mi(`measure'`time') & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d) `wgt', dyads(minMP maxMP)
				est sto Het`time'`r'
				local obsHet`time'`r' = e(N)
				local numReg = 3
				matrix varMatrix = e(V)
				
				// prepare coefficient, standard error, p-value for export					
				local coef_`time'`r'_in = string(_b[xinresid],"%9.2g")
				local coef_`time'`r'_mid = string(_b[xmidresid],"%9.2g")				
				local coef_`time'`r'_out = string(_b[xoutresid],"%9.2g")
				local se_`time'`r'_in = _se[xinresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
				local se_`time'`r'_mid = _se[xmidresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))				
				local se_`time'`r'_out = _se[xoutresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
				local t_df = `numN'-`numReg'-`numDummy'-1
				local pval_`time'`r'_in =  string(ttail(`t_df',abs(`coef_`time'`r'_in'/`se_`time'`r'_in'))*2,"%9.2g")
				local pval_`time'`r'_mid =  string(ttail(`t_df',abs(`coef_`time'`r'_mid'/`se_`time'`r'_mid'))*2,"%9.2g")				
				local pval_`time'`r'_out =  string(ttail(`t_df',abs(`coef_`time'`r'_out'/`se_`time'`r'_out'))*2,"%9.2g")
				drop yresid xinresid xmidresid xoutresid
				
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme_in = 0
					local Nbeta_extreme_mid = 0					
					local Nbeta_extreme_out = 0
					forv i=1/`runsRI' {
						areg `measure'`time' neighborRI`i'XpartyVClose neighborRI`i'XpartyMid neighborRI`i'XpartyVFar, absorb(sessionXpairStrataXpartyPair)
						local absCoefIn = abs(_b[neighborRI`i'XpartyVClose])
						local absCoefMid = abs(_b[neighborRI`i'XpartyMid])						
						local absCoefOut = abs(_b[neighborRI`i'XpartyVFar])
						local Nbeta_extreme_in = `Nbeta_extreme_in' + (`absCoefIn' >= abs(`coef_`time'`r'_in')) // + 1 every time see more extreme beta
						local Nbeta_extreme_mid = `Nbeta_extreme_mid' + (`absCoefMid' >= abs(`coef_`time'`r'_mid')) 						
						local Nbeta_extreme_out = `Nbeta_extreme_out' + (`absCoefOut' >= abs(`coef_`time'`r'_out')) 
					}
					local pval_`time'`r'_RIin = string(`Nbeta_extreme_in'/`runsRI',"%9.2g")
					local pval_`time'`r'_RImid = string(`Nbeta_extreme_mid'/`runsRI',"%9.2g")						
					local pval_`time'`r'_RIout = string(`Nbeta_extreme_out'/`runsRI',"%9.2g")	
				}
				else {
					local pval_`time'`r'_RIin = 999
					local pval_`time'`r'_RImid = 999	
					local pval_`time'`r'_RIout = 999										
				}					
				
				// get stars for all pvals
				foreach y in in mid out RIin RImid RIout {
					local pstar_`time'`r'_`y' = "" // reset p-value
					if `pval_`time'`r'_`y''<0.10 & `pval_`time'`r'_`y''>=0.05 local pstar_`time'`r'_`y' = "*"
					if `pval_`time'`r'_`y''<0.05 & `pval_`time'`r'_`y''>=0.01 local pstar_`time'`r'_`y' = "**"
					if `pval_`time'`r'_`y''<0.01 local pstar_`time'`r'_`y' = "***"
				}
				
				// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
				foreach y in in mid out {
					if `pval_`time'`r'_`y''<0.001 local pval_`time'`r'_`y' = "<0.001" 
				}					
		}
	}
		
	// Output Heterogeneous Effects Table
	local tex "Neighbor $\times$ Distant Other-Party & `coef_1_out' & `coef_2_out' & `coef_Lead1_out' & `coef_Lead2_out' & `coef_Lag1_out' & `coef_Lag2_out' \\"
	local tex "`tex' & [`pval_1_out']`pstar_1_out' & [`pval_2_out']`pstar_2_out' & [`pval_Lead1_out']`pstar_Lead1_out' & [`pval_Lead2_out']`pstar_Lead2_out' & [`pval_Lag1_out']`pstar_Lag1_out' & [`pval_Lag2_out']`pstar_Lag2_out' \\"
	local tex "`tex' & \{`pval_1_RIout'\}`pstar_1_RIout' & \{`pval_2_RIout'\}`pstar_2_RIout' & \{`pval_Lead1_RIout'\}`pstar_Lead1_RIout' & \{`pval_Lead2_RIout'\}`pstar_Lead2_RIout' & \{`pval_Lag1_RIout'\}`pstar_Lag1_RIout' & \{`pval_Lag2_RIout'\}`pstar_Lag2_RIout' \\ \\[-5pt]"		
	
	local tex "`tex' Neighbor $\times$ Middle Other-Party & `coef_1_mid' & `coef_2_mid' & `coef_Lead1_mid' & `coef_Lead2_mid' & `coef_Lag1_mid' & `coef_Lag2_mid' \\"
	local tex "`tex' & [`pval_1_mid']`pstar_1_mid' & [`pval_2_mid']`pstar_2_mid' & [`pval_Lead1_mid']`pstar_Lead1_mid' & [`pval_Lead2_mid']`pstar_Lead2_mid' & [`pval_Lag1_mid']`pstar_Lag1_mid' & [`pval_Lag2_mid']`pstar_Lag2_mid' \\"
	local tex "`tex' & \{`pval_1_RImid'\}`pstar_1_RImid' & \{`pval_2_RImid'\}`pstar_2_RImid' & \{`pval_Lead1_RImid'\}`pstar_Lead1_RImid' & \{`pval_Lead2_RImid'\}`pstar_Lead2_RImid' & \{`pval_Lag1_RImid'\}`pstar_Lag1_RImid' & \{`pval_Lag2_RImid'\}`pstar_Lag2_RImid' \\ \\[-5pt]"		
	
	local tex "`tex' Neighbor $\times$ Close Other-Party & `coef_1_in' & `coef_2_in' & `coef_Lead1_in' & `coef_Lead2_in'  & `coef_Lag1_in' & `coef_Lag2_in' \\"
	local tex "`tex'  & [`pval_1_in']`pstar_1_in' & [`pval_2_in']`pstar_2_in' & [`pval_Lead1_in']`pstar_Lead1_in' & [`pval_Lead2_in']`pstar_Lead2_in' & [`pval_Lag1_in']`pstar_Lag1_in' & [`pval_Lag2_in']`pstar_Lag2_in' \\"
	local tex "`tex' & \{`pval_1_RIin'\}`pstar_1_RIin' & \{`pval_2_RIin'\}`pstar_2_RIin' & \{`pval_Lead1_RIin'\}`pstar_Lead1_RIin' & \{`pval_Lead2_RIin'\}`pstar_Lead2_RIin' & \{`pval_Lag1_RIin'\}`pstar_Lag1_RIin' & \{`pval_Lag2_RIin'\}`pstar_Lag2_RIin' \\ \\[-5pt] \hline"				
	
	local tex "`tex' Observations & `obsHet1' & `obsHet2' & `obsHetLead1' & `obsHetLead2' & `obsHetLag1' & `obsHetLag2' \\" 
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `meanLead1' & `meanLead2' & `meanLag1' & `meanLag2' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sdLead1' & `sdLead2' & `sdLag1' & `sdLag2' \\ \toprule" 
	local tex "`tex' \multicolumn{7}{p{20cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' Sample includes only different-party pairs. `compliance' `similarity'"
	local tex "`tex' `neighbor'"
	local tex "`tex' Distant Other-Party is a dummy variable equal to one if the difference between the left-right score (from ParlGov) of the two parties in a pair is in the top tercile. Middle Other-Party is a dummy variable for the middle tercile, Close Other-Party is a dummy variable for the bottom tercile."	
	local tex "`tex' `dyadSquare' `riCurly'"
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `plab'}"
	local tex "`tex' \\ \end{tabular} }"
	
	esttab Het1 Het2 HetLead1 HetLead2 HetLag1 HetLag2 using "`tablefolder'//pairVotePartyTerc.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("\specialcell{Contemporaneous\\Effect (t)}" "\specialcell{One Year\\Later (t+1)}" ///
			"\specialcell{Previous Year\\(Placebo) (t-1)}", pattern(1 0 1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Compliance\\(1)}" "\specialcell{Similarity\\(2)}" ///
	"\specialcell{Compliance\\(3)}" "\specialcell{Similarity\\(4)}" ///	
	"\specialcell{Compliance\\(5)}" "\specialcell{Similarity\\(6)}")		
				
	timer off 34
	disp "DONE dyadVotePartyTerc"
	timer list 34				
}
if `dyadmpSims'==1 { // Dyadic and MP-session-level voting neighbour effects using simulated data
	
	timer on 35
	
	** Switches
	local draws		50
	local pcue		30 // 1, 2, 3, 5, 7, 10, 15, 20, 30
	local pcuelab 	"`pcue'perc"
	
	** Merge all RI draws
	use "`votingsimfolder'/sim_MP_dyad_RI1to10_`pcuelab'", clear
	merge 1:1 session_id MP_id MP_id_d using "`votingsimfolder'/sim_MP_dyad_RI11to20_`pcuelab'", assert(3) nogen
	merge 1:1 session_id MP_id MP_id_d using "`votingsimfolder'/sim_MP_dyad_RI21to30_`pcuelab'", assert(3) nogen
	merge 1:1 session_id MP_id MP_id_d using "`votingsimfolder'/sim_MP_dyad_RI31to40_`pcuelab'", assert(3) nogen
	merge 1:1 session_id MP_id MP_id_d using "`votingsimfolder'/sim_MP_dyad_RI41to50_`pcuelab'", assert(3) nogen	
	tempfile simdata
	save `simdata'
	
	** Dyad-level
	use `datamod', clear
	merge 1:1 session_id MP_id MP_id_d using `simdata', keep(1 3) nogen
	
	forv i=1/`draws' {		
		
		/* Hetero: party ingroup and outgroup effect */
	
		// residualize			
		qui areg samevote4c`i'_VS_sim if !mi(neighborRI`i'Xsame`hetero') & !mi(neighborRI`i'Xdiff`hetero') ///
			& !mi(samevote4c`i'_VS_sim) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair)
		local numN = e(N)
		local numDummy = e(df_a) 
		predict yresid, residuals
		
		qui areg neighborRI`i'Xsame`hetero' if !mi(neighborRI`i'Xsame`hetero') & !mi(neighborRI`i'Xdiff`hetero') ///
			& !mi(samevote4c`i'_VS_sim) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
		local numN2 = e(N)
		predict xinresid, residuals
		assert `numN' == `numN2'
		
		qui areg neighborRI`i'Xdiff`hetero' if !mi(neighborRI`i'Xsame`hetero') & !mi(neighborRI`i'Xdiff`hetero') ///
			& !mi(samevote4c`i'_VS_sim) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
		predict xoutresid, residuals
		
		// run regression
		xi: regdyad2 yresid xinresid xoutresid if !mi(neighborRI`i'Xsame`hetero') & !mi(neighborRI`i'Xdiff`hetero') ///
			& !mi(samevote4c`i'_VS_sim) & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d), dyads(minMP maxMP)
		local numReg = 2
			
		// prepare coefficient, standard error, p-value for export					
		local coef_out`i' = _b[xoutresid]
		local se_out`i' = _se[xoutresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
		local t_df`i' = `numN'-`numReg'-`numDummy'-1
		local pval_out`i' =  ttail(`t_df`i'',abs(`coef_out`i''/`se_out`i''))*2
		
		drop yresid xinresid xoutresid
	}
	
	
	** Merge all draws for MP-level data
	use "`votingsimfolder'/sim_MP_sess_RI1to10_`pcuelab'", clear
	merge 1:1 session_id MP_id using "`votingsimfolder'/sim_MP_sess_RI11to20_`pcuelab'", assert(3) nogen
	merge 1:1 session_id MP_id using "`votingsimfolder'/sim_MP_sess_RI21to30_`pcuelab'", assert(3) nogen
	merge 1:1 session_id MP_id using "`votingsimfolder'/sim_MP_sess_RI31to40_`pcuelab'", assert(3) nogen
	merge 1:1 session_id MP_id using "`votingsimfolder'/sim_MP_sess_RI41to50_`pcuelab'", assert(3) nogen	
	tempfile simdatamp
	save `simdatamp'	
		
	** MP-level
	use "`votingfolder'/MP_session_analysis", clear
	merge 1:1 MP_id session_id using `simdatamp', keep(3) nogen
	
	// Define sessions that we will use
	g sampleSession = (!specialSession) & (!shortSession) & !(session_id == 149)
	
	// Estimate with simulated data
	forv i=1/`draws' {
		areg rebelWeak`i'_VS_sim fracOther`Hetero'NeighborRI`i' if sampleSession, absorb(strataSessionPartyFE) cluster(MP_id)
		
		local coef_gen`i' = _b[fracOther`Hetero'NeighborRI`i']
		local se_gen`i' = _se[fracOther`Hetero'NeighborRI`i']
		local pval_gen`i' = ttail(e(df_r),abs(`coef_gen`i''/`se_gen`i''))*2			
	}
	
	
	** Now summarise results of simulation
	clear
	set obs `draws'
	g draw_num = _n
	g dyad_beta = .
	g dyad_pval =.
	g gen_beta = .
	g gen_pval = .
	
	forv i=1/`draws' {
		replace dyad_beta = `coef_out`i'' in `i'
		replace dyad_pval = `pval_out`i'' in `i'
		replace gen_beta = `coef_gen`i'' in `i'
		replace gen_pval = `pval_gen`i'' in `i'
	}
	
	foreach x in dyad gen {
		g `x'_reject = `x'_pval<0.05
	}
	
	g prob_influence = `pcue'
	
	save "`votingsimfolder'/sim_results_RI`draws'_`pcuelab'", replace
	
	timer off 35
	timer list 35	
}
if `dyadmpSimsTab'==1 { // Create the table that summarizes the results of the simulations
	
	timer on 36
	
	local draws = 50
	clear

	foreach x in 1 2 3 5 7 10 15 20 30 {
		append using "`votingsimfolder'/sim_results_RI`draws'_`x'perc"
	}
	
	** Regs just to make table
	reg dyad_beta gen_beta
	est sto TmpReg

	foreach x in 1 2 3 5 7 10 15 20 30 {
		foreach y in dyad gen {
			qui su `y'_beta if prob_influence==`x', det
			local `y'_beta`x' = string(r(mean),"%9.3g")		
			
			qui su `y'_reject if prob_influence==`x'
			local `y'_reject`x' = string(100*r(mean),"%9.3g")
		}
	}

	local tex "Actual & .0096 & -.00036 \\"
	local tex "`tex' & p<0.001 & p=0.66 \\ \\[-5pt]" 	
	local tex "`tex' 1 & `dyad_beta1' & `gen_beta1' \\"
	local tex "`tex' & `dyad_reject1'\% & `gen_reject1'\% \\ \\[-5pt]"
	local tex "`tex' 2 & `dyad_beta2' & `gen_beta2' \\"
	local tex "`tex' & `dyad_reject2'\% & `gen_reject2'\% \\ \\[-5pt]"
	local tex "`tex' 3 & `dyad_beta3' & `gen_beta3' \\"
	local tex "`tex' & `dyad_reject3'\% & `gen_reject3'\% \\ \\[-5pt]"
	local tex "`tex' 5 & `dyad_beta5' & `gen_beta5' \\"
	local tex "`tex' & `dyad_reject5'\% & `gen_reject5'\% \\ \\[-5pt]"
	local tex "`tex' 7 & `dyad_beta7' & `gen_beta7' \\"
	local tex "`tex' & `dyad_reject7'\% & `gen_reject7'\% \\ \\[-5pt]"
	local tex "`tex' 10 & `dyad_beta10' & `gen_beta10' \\"
	local tex "`tex' & `dyad_reject10'\% & `gen_reject10'\% \\ \\[-5pt]"
	local tex "`tex' 15 & `dyad_beta15' & `gen_beta15' \\"
	local tex "`tex' & `dyad_reject15'\% & `gen_reject15'\% \\ \\[-5pt]"
	local tex "`tex' 20 & `dyad_beta20' & `gen_beta20' \\"
	local tex "`tex' & `dyad_reject20'\% & `gen_reject20'\% \\ \toprule \end{tabular}}"
	
	*local tex "`tex' 30 & `dyad_beta30' & `gen_beta30' \\"
	*local tex "`tex' & `dyad_reject30'\% & `gen_reject30'\% \\ " 

	esttab TmpReg TmpReg using "`tablefolder'//simsRI`draws'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mtitles("\specialcell{Dyadic Effects\\\$\gamma_2$}" "\specialcell{General Effects\\\$\beta$}")		
		
	timer off 36
	timer list 36
}
if `billTopic'==1 { // Dyadic voting results by bill topic (2001 onwards only)
	
	timer on 37
	
	// find number of bills belonging to each type
	use "`votingintfolder'/bill_type/bill_cat_clean", clear
	isid session_id bill_id
	foreach x in industry foreign econ health law educ community transport admin relig environ {
		qui su `x'_cat
		local `x'Perc = string(100*r(mean),"%9.1g")
	}

	forv r = 1/11 {	// Type of voting/cosponsorship measure?
		local measure: word `r' of "voteSim4c_industry" "voteSim4c_foreign" "voteSim4c_econ" "voteSim4c_health" "voteSim4c_law" "voteSim4c_educ" "voteSim4c_community" "voteSim4c_transport" "voteSim4c_admin" "voteSim4c_relig" "voteSim4c_environ"
			
			dis "`measure'"
			use `datamod', clear
			
			keep session_id voteSim?c* *RI* minMP maxMP ///
			pairStrata samecoalition sameparty ///
			samegender specialSession shortSession firstSession lastSession leadShortIncomplete	party_id party_id_d	///
			sessionXpairStrataXpartyPair
			
			/* Neighborhood effect */
			
			// residualize
			qui areg `measure' if !mi(`measure') & !mi(neighborRI0) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
			local numN = e(N)
			local numDummy = e(df_a) 
			predict yresid, residuals
			qui areg neighborRI0 if !mi(`measure') & !mi(neighborRI0) & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
			local numN2 = e(N)
			predict xresid, residuals
			assert `numN' == `numN2'
			
			// run regression
			xi: regdyad2 yresid xresid if !mi(`measure') & !mi(neighborRI0) & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d), ///
				dyads(minMP maxMP)
			est sto M`r'
			local obs`r' = e(N)
			qui su `measure' if e(sample)==1
			local mean`r' = string(r(mean),"%9.2g")
			local sd`r' = string(r(sd),"%9.2g")
			local sd`r'raw = `r(sd)'
			local numReg = 1
			
			// prepare coefficient, standard error, p-value for export
			local coef_`r'_av = _b[xresid]
			local se_`r'_av = string(_se[xresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1)),"%9.2g")
			local t_df = `numN'-`numReg'-`numDummy'-1
			local pval_`r'_av = string(ttail(`t_df',abs(`coef_`r'_av'/`se_`r'_av'))*2,"%9.2g")					
			drop yresid xresid	
			
			// randomization inference p-values
			if `RI'==1 {
				local Nbeta_extreme = 0
				forv i=1/`runsRI' {
					areg `measure' neighborRI`i', absorb(sessionXpairStrataXpartyPair)
					local absCoef = abs(_b[neighborRI`i'])
					local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`r'_av')) // + 1 every time see more extreme beta
				}
				local pval_`r'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
			}
			else {
				local pval_`r'_RI = 999
			}				
			
			/* Hetero: party ingroup and outgroup effect */
			
			// residualize			
			qui areg `measure' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair)
			local numN = e(N)
			local numDummy = e(df_a) 
			predict yresid, residuals
			
			qui areg neighborRI0Xsame`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
			local numN2 = e(N)
			predict xinresid, residuals
			assert `numN' == `numN2'
			qui areg neighborRI0Xdiff`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') & !mi(pairStrata), absorb(sessionXpairStrataXpartyPair) 
			predict xoutresid, residuals
			
			// run regression
			xi: regdyad2 yresid xinresid xoutresid if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') & !mi(`measure') ///
				& !mi(pairStrata) & !mi(party_id) & !mi(party_id_d), dyads(minMP maxMP)
			est sto Het`r'
			local obsHet`r' = string(e(N),"%9.0gc")
			local numReg = 2
			matrix varMatrix = e(V)
			
			// prepare coefficient, standard error, p-value for export					
			local coef_`r'_in = string(_b[xinresid],"%9.2g")
			local coef_`r'_out = string(_b[xoutresid],"%9.2g")
			local se_`r'_in = _se[xinresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
			local se_`r'_out = _se[xoutresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
			local t_df = `numN'-`numReg'-`numDummy'-1
			local pval_`r'_in =  string(ttail(`t_df',abs(`coef_`r'_in'/`se_`r'_in'))*2,"%9.2g")
			local pval_`r'_out =  string(ttail(`t_df',abs(`coef_`r'_out'/`se_`r'_out'))*2,"%9.2g")
			
			// pval calculation for the difference
			local diff = `coef_`r'_in' - `coef_`r'_out'
			local se_`r'_diff = sqrt(varMatrix[1,1]+varMatrix[2,2]-2*varMatrix[2,1])
			local pval_`r'_diff =  string(ttail(`t_df',abs(`diff'/`se_`r'_diff'))*2,"%9.2g")
			drop yresid xinresid xoutresid
			
			// randomization inference p-values
			if `RI'==1 {
				local Nbeta_extreme_in = 0
				local Nbeta_extreme_out = 0
				local Nbeta_extreme_diff = 0
				forv i=1/`runsRI' {
					areg `measure' neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero', absorb(sessionXpairStrataXpartyPair)
					local absCoefIn = abs(_b[neighborRI`i'Xsame`hetero'])
					local absCoefOut = abs(_b[neighborRI`i'Xdiff`hetero'])
					local Nbeta_extreme_in = `Nbeta_extreme_in' + (`absCoefIn' >= abs(`coef_`r'_in')) // + 1 every time see more extreme beta
					local Nbeta_extreme_out = `Nbeta_extreme_out' + (`absCoefOut' >= abs(`coef_`r'_out')) 
				
					// for difference between in vs out p-value we need separate regressions, using "modified" outcome
					g `measure'_m = `measure'
					replace `measure'_m = `measure'_m + `coef_`r'_av' if neighborRI`i' == 1 & neighborRI0 == 0
					replace `measure'_m = `measure'_m - `coef_`r'_av' if neighborRI`i' == 0 & neighborRI0 == 1	
					
					// run regression with modified outcome
					areg `measure'_m neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero', absorb(sessionXpairStrataXpartyPair)
					local absCoefDiff = abs(_b[neighborRI`i'Xsame`hetero']-_b[neighborRI`i'Xdiff`hetero'])
					local Nbeta_extreme_diff = `Nbeta_extreme_diff' + (`absCoefDiff' >= abs(`diff')) 
					drop `measure'_m
				}
				local pval_`r'_RIin = string(`Nbeta_extreme_in'/`runsRI',"%9.2g")
				local pval_`r'_RIout = string(`Nbeta_extreme_out'/`runsRI',"%9.2g")	
				local pval_`r'_RIdiff = string(`Nbeta_extreme_diff'/`runsRI',"%9.2g")	
			}
			else {
				local pval_`r'_RIin = 999
				local pval_`r'_RIout = 999	
				local pval_`r'_RIdiff = 999										
			}					
			
			// get stars for all pvals
			foreach y in av in out diff RI RIin RIout RIdiff {
				local pstar_`r'_`y' = "" // reset p-value
				if `pval_`r'_`y''<0.10 & `pval_`r'_`y''>=0.05 local pstar_`r'_`y' = "*"
				if `pval_`r'_`y''<0.05 & `pval_`r'_`y''>=0.01 local pstar_`r'_`y' = "**"
				if `pval_`r'_`y''<0.01 local pstar_`r'_`y' = "***"
			}
	
			// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
			foreach y in av in out diff {
				if `pval_`r'_`y''<0.001 local pval_`r'_`y' = "<0.001" 
			}						
	}
			
	// Output Heterogeneous Effects Table
	local tex "Neighbor $\times$ Different `Hetero' & `coef_1_out' & `coef_2_out' & `coef_3_out' & `coef_4_out' & `coef_5_out' & `coef_6_out' & `coef_7_out' & `coef_8_out' & `coef_9_out' & `coef_10_out' & `coef_11_out' \\"
	local tex "`tex' & [`pval_1_out']`pstar_1_out' & [`pval_2_out']`pstar_2_out' & [`pval_3_out']`pstar_3_out' & [`pval_4_out']`pstar_4_out' & [`pval_5_out']`pstar_5_out' & [`pval_6_out']`pstar_6_out' & [`pval_7_out']`pstar_7_out' & [`pval_8_out']`pstar_8_out' & [`pval_9_out']`pstar_9_out' & [`pval_10_out']`pstar_10_out' & [`pval_11_out']`pstar_11_out' \\"
	local tex "`tex' & \{`pval_1_RIout'\}`pstar_1_RIout' & \{`pval_2_RIout'\}`pstar_2_RIout' & \{`pval_3_RIout'\}`pstar_3_RIout' & \{`pval_4_RIout'\}`pstar_4_RIout' & \{`pval_5_RIout'\}`pstar_5_RIout' & \{`pval_6_RIout'\}`pstar_6_RIout' & \{`pval_7_RIout'\}`pstar_7_RIout' & \{`pval_8_RIout'\}`pstar_8_RIout' & \{`pval_9_RIout'\}`pstar_9_RIout' & \{`pval_10_RIout'\}`pstar_10_RIout' & \{`pval_11_RIout'\}`pstar_11_RIout' \\ \\[-5pt]"		
	
	local tex "`tex' Neighbor $\times$ Same `Hetero' & `coef_1_in' & `coef_2_in' & `coef_3_in' & `coef_4_in'  & `coef_5_in' & `coef_6_in' & `coef_7_in' & `coef_8_in' & `coef_9_in' & `coef_10_in' & `coef_11_in' \\"
	local tex "`tex'  & [`pval_1_in']`pstar_1_in' & [`pval_2_in']`pstar_2_in' & [`pval_3_in']`pstar_3_in' & [`pval_4_in']`pstar_4_in' & [`pval_5_in']`pstar_5_in' & [`pval_6_in']`pstar_6_in' & [`pval_7_in']`pstar_7_in' & [`pval_8_in']`pstar_8_in' & [`pval_9_in']`pstar_9_in' & [`pval_10_in']`pstar_10_in' & [`pval_11_in']`pstar_11_in' \\"
	local tex "`tex' & \{`pval_1_RIin'\}`pstar_1_RIin' & \{`pval_2_RIin'\}`pstar_2_RIin' & \{`pval_3_RIin'\}`pstar_3_RIin' & \{`pval_4_RIin'\}`pstar_4_RIin' & \{`pval_5_RIin'\}`pstar_5_RIin' & \{`pval_6_RIin'\}`pstar_6_RIin' & \{`pval_7_RIin'\}`pstar_7_RIin' & \{`pval_8_RIin'\}`pstar_8_RIin' & \{`pval_9_RIin'\}`pstar_9_RIin' & \{`pval_10_RIin'\}`pstar_10_RIin' & \{`pval_11_RIin'\}`pstar_11_RIin' \\ \\[-5pt]"		
				
	local tex "`tex' Same = Different & [`pval_1_diff']`pstar_1_diff' & [`pval_2_diff']`pstar_2_diff' & [`pval_3_diff']`pstar_3_diff' & [`pval_4_diff']`pstar_4_diff' & [`pval_5_diff']`pstar_5_diff' & [`pval_6_diff']`pstar_6_diff' & [`pval_7_diff']`pstar_7_diff' & [`pval_8_diff']`pstar_8_diff' & [`pval_9_diff']`pstar_9_diff' & [`pval_10_diff']`pstar_10_diff' & [`pval_11_diff']`pstar_11_diff' \\"
	local tex "`tex' & \{`pval_1_RIdiff'\}`pstar_1_RIdiff' & \{`pval_2_RIdiff'\}`pstar_2_RIdiff' & \{`pval_3_RIdiff'\}`pstar_3_RIdiff' & \{`pval_4_RIdiff'\}`pstar_4_RIdiff' & \{`pval_5_RIdiff'\}`pstar_5_RIdiff' & \{`pval_6_RIdiff'\}`pstar_6_RIdiff' & \{`pval_7_RIdiff'\}`pstar_7_RIdiff' & \{`pval_8_RIdiff'\}`pstar_8_RIdiff' & \{`pval_9_RIdiff'\}`pstar_9_RIdiff' & \{`pval_10_RIdiff'\}`pstar_10_RIdiff' & \{`pval_11_RIdiff'\}`pstar_11_RIdiff' \\ \\[-5pt] \hline"				
	
	local tex "`tex' Observations & `obsHet1' & `obsHet2' & `obsHet3' & `obsHet4' & `obsHet5' & `obsHet6' & `obsHet7' & `obsHet8' & `obsHet9' & `obsHet10' & `obsHet11' \\" 
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y & Y & Y & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `mean3' & `mean4' & `mean5' & `mean6' & `mean7' & `mean8' & `mean9' & `mean10' & `mean11' \\"  
	local tex "`tex' Percentage of All Bills & `industryPerc' & `foreignPerc' & `econPerc' & `healthPerc' & `lawPerc' & `educPerc' & `communityPerc' & `transportPerc' & `adminPerc' & `religPerc' & `environPerc' \\ \toprule" 
		
	local tex "`tex' \multicolumn{12}{p{24cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `similarity'"
	local tex "`tex' We include only the contested votes in which the share of MPs voting the modal vote is less than the median among all votes."
	local tex "`tex' The results are shown separately for voting on legislation belonging to the following categories: (1) Industry, (2) Foreign Relations, (3) Economic Management, (4) Health, (5) Law and Justice, (6) Education and Culture, (7) Community Issues, (8) Transport, (9) Administration and Local Governance, (10) Religion, and (11) Environment. Each piece of legislation may belong to more than one category (explaining why the percentage of all bills does not sum to 100\%). Sample includes only 2001-02 session onwards as bill topic data only goes back to 2001-02."
	local tex "`tex' `neighbor' `samehet'"
	local tex "`tex' `dyadSquare' `riCurly'"
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `plab'}"
	local tex "`tex' \\ \end{tabular} }"
	
	esttab Het1 Het2 Het3 Het4 Het5 Het6 Het7 Het8 Het9 Het10 Het11 using "`tablefolder'//billTopic`Hetero'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("Contemporaneous Effect (t) on Similarity", pattern(1 0 0 0 0 0 0 0 0 0 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Industry\\(1)}" "\specialcell{Foreign\\(2)}" ///
	"\specialcell{Economic\\(3)}" "\specialcell{Health\\(4)}" ///
	"\specialcell{Law\\(5)}" "\specialcell{Educ.\\(6)}" "\specialcell{Community\\(7)}" "\specialcell{Transport\\(8)}" ///
	"\specialcell{Admin.\\(9)}" "\specialcell{Religion\\(10)}" "\specialcell{Environ.\\(11)}")
	
	timer off 37
	disp "DONE billTopic"
	timer list 37
}
if `mpVoteAltOutcome'==1 { // MP-session-level voting results with 2 alternative outcomes

	timer on 38
	use "`votingfolder'/MP_session_analysis", clear
	
	/* Calculate weights */
	
	if `reweight'==1 {
		bys strataSessionPartyFE: egen probHalf = mean(halfOther`Hetero'NeighborRI0)
		bys strataSessionPartyFE: egen probAll = mean(allOther`Hetero'NeighborRI0)
		g noOther`Hetero'NeighborRI0 = halfOther`Hetero'NeighborRI0==0 & allOther`Hetero'NeighborRI0==0 & !mi(halfOther`Hetero'NeighborRI0)
		bys strataSessionPartyFE: egen probNone = mean(noOther`Hetero'NeighborRI0)
		
		g invProb = 1/probHalf if halfOther`Hetero'NeighborRI0==1
		replace invProb = 1/probAll if allOther`Hetero'NeighborRI0==1
		replace invProb = 1/probNone if noOther`Hetero'NeighborRI0==1
	}

	// Define sessions that we will use
	g sampleSession = (!specialSession) & (!shortSession) & !(session_id == 149)
	g sampleSessionLag = !inlist(session_id,115,119,120,124,125,129,130,134,135,137,138,142,143,146,147,148,149)
	g sampleSessionLead = !inlist(session_id,118,119,123,124,128,129,133,134,136,137,141,142,145,146,147,148,149)

	// Three panels: contemporary, lead, lag (placebo)
	forv k = 1/3 { 		// Time period?
		local time: word `k' of "Lag" "" "Lead"	
		
		preserve
			drop if mi(voteDiff4c`time')
	
			forv r = 1/2 {	// Type of voting/cosponsorship measure?
				local measure: word `r' of "non_compliance_alt" "voteDiff4c"
				
				areg `measure'`time' fracOther`Hetero'NeighborRI0 if sampleSession`time' `wgt', absorb(strataSessionPartyFE) cluster(MP_id)
				est sto MP`time'`r'
				local obs`time'`r' = e(N)	
				
				// summary stats
				qui su `measure'`time' if e(sample)==1
				local mean`time'`r' = string(r(mean),"%9.2g")
				local sd`time'`r' = string(r(sd),"%9.2g")
				local sd`time'`r'raw = `r(sd)'	
				
				// prepare coefficient, standard error, p-value for export
				local coef_`time'`r'_av = _b[fracOther`Hetero'NeighborRI0]
				local coef_`time'`r'_avs = string(_b[fracOther`Hetero'NeighborRI0],"%9.2g")			
				local se_`time'`r'_av = string(_se[fracOther`Hetero'NeighborRI0],"%9.2g")
				local pval_`time'`r'_av = string(ttail(e(df_r),abs(`coef_`time'`r'_av'/`se_`time'`r'_av'))*2,"%9.2g")	
								
				// diff-party point estimate and 95% confidence interval for figure
				if `k'==2 {
					local mp_`r'_out = `coef_`r'_av'/`sd`r'raw'
					local mpl_`r'_out = (`coef_`r'_av' - invttail(e(df_r),0.025)*`se_`r'_av')/`sd`r'raw'
					local mpu_`r'_out = (`coef_`r'_av' + invttail(e(df_r),0.025)*`se_`r'_av')/`sd`r'raw'
				}		
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme = 0
					forv i=1/`runsRI' {
						
						if `reweight'==1 {
							drop probHalf probAll probNone invProb
							
							bys strataSessionPartyFE: egen probHalf = mean(halfOther`Hetero'NeighborRI`i')
							bys strataSessionPartyFE: egen probAll = mean(allOther`Hetero'NeighborRI`i')
							g noOther`Hetero'NeighborRI`i' = halfOther`Hetero'NeighborRI`i'==0 & ///
									allOther`Hetero'NeighborRI`i'==0 & !mi(halfOther`Hetero'NeighborRI`i')
							bys strataSessionPartyFE: egen probNone = mean(noOther`Hetero'NeighborRI`i')
							
							g invProb = 1/probHalf if halfOther`Hetero'NeighborRI`i'==1
							replace invProb = 1/probAll if allOther`Hetero'NeighborRI`i'==1
							replace invProb = 1/probNone if noOther`Hetero'NeighborRI`i'==1
						}
						
						areg `measure'`time' fracOther`Hetero'NeighborRI`i' if sampleSession`time' `wgt', absorb(strataSessionPartyFE)
						local absCoef = abs(_b[fracOther`Hetero'NeighborRI`i'])
						local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`time'`r'_av')) // + 1 every time see more extreme beta
					}
					local pval_`time'`r'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
				}
				else {
					local pval_`time'`r'_RI = 999
				}
				
				// get stars for all pvals
				foreach y in av RI {
					local pstar_`time'`r'_`y' = "" // reset p-value
					if `pval_`time'`r'_`y''<0.10 & `pval_`time'`r'_`y''>=0.05 local pstar_`time'`r'_`y' = "*"
					if `pval_`time'`r'_`y''<0.05 & `pval_`time'`r'_`y''>=0.01 local pstar_`time'`r'_`y' = "**"
					if `pval_`time'`r'_`y''<0.01 local pstar_`time'`r'_`y' = "***"
				}	
				
				// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
				if `pval_`time'`r'_av'<0.001 local pval_`time'`r'_av = "<0.001" 			
			}
		restore
	}
	
	// Output Main Effects Table
	estout MP1 MP2 MPLead1 MPLead2 MPLag1 MPLag2 using "`tablefolder'//mpVoteAlt`Hetero'A`wgtlab'.tex", style(tex) replace ///
	keep(fracOther`Hetero'NeighborRI0) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(fracOther`Hetero'NeighborRI0 "Proportion Other-`Hetero' Neighbor")

	*local tex "\input{mpVote`Hetero'A`wgtlab'} \vspace{-1.2em} \\"
	local tex "Proportion Other-`Hetero' & `coef_1_avs' & `coef_2_avs' & `coef_Lead1_avs' & `coef_Lead2_avs' & `coef_Lag1_avs' & `coef_Lag2_avs' \\"
	local tex "`tex' Neighbor & (`se_1_av') & (`se_2_av') & (`se_Lead1_av') & (`se_Lead2_av') & (`se_Lag1_av') & (`se_Lag2_av') \\"
	local tex "`tex' & [`pval_1_av']`pstar_1_av' & [`pval_2_av']`pstar_2_av' & [`pval_Lead1_av']`pstar_Lead1_av' & [`pval_Lead2_av']`pstar_Lead2_av' & [`pval_Lag1_av']`pstar_Lag1_av' & [`pval_Lag2_av']`pstar_Lag2_av' \\"
	local tex "`tex' & \{`pval_1_RI'\}`pstar_1_RI' & \{`pval_2_RI'\}`pstar_2_RI' &  \{`pval_Lead1_RI'\}`pstar_Lead1_RI' & \{`pval_Lead2_RI'\}`pstar_Lead2_RI' & \{`pval_Lag1_RI'\}`pstar_Lag1_RI' & \{`pval_Lag2_RI'\}`pstar_Lag2_RI' \\ \\[-5pt] \hline"	
	
	local tex "`tex' Observations & `obs1' & `obs2' & `obsLead1' & `obsLead2' & `obsLag1' & `obsLag2' \\"
	local tex "`tex' Session $\times$ Party $\times$ Strata FE & Y & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `meanLead1' & `meanLead2' & `meanLag1' & `meanLag2' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sdLead1' & `sdLead2' & `sdLag1' & `sdLag2' \\ \toprule" 
	
	local tex "`tex' \multicolumn{7}{p{17.8cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `wgttabnote'`noncompliancealtMP' `differenceMP' `propothMP'"
	local tex "`tex' `mpSEp' `riCurly'"
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDefMP' `plab'}"	
	local tex "`tex' \\ \end{tabular} }"
		
	esttab MP1 MP2 MPLead1 MPLead2 MPLag1 MPLag2 using "`tablefolder'//mpVoteAlt`Hetero'`wgtlab'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("\specialcell{Contemporaneous\\Effect (t)}" "\specialcell{One Year\\Later (t+1)}" ///
			"\specialcell{Previous Year\\(Placebo) (t-1)}", pattern(1 0 1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Party\\Non-\\Compliance\\(1)}" "\specialcell{\\Leader\\Diff.\\(2)}" ///
	"\specialcell{Party\\Non-\\Compliance\\(3)}" "\specialcell{\\Leader\\Diff.\\(4)}" ///	
	"\specialcell{Party\\Non-\\Compliance\\(5)}" "\specialcell{\\Leader\\Diff.\\(6)}")		
	
	timer off 38
	disp "DONE mpVoteAltOutcome"
	timer list 38
}
if `mpAbsentAbstain'==1 { // MP-session-level voting results, effects on absence and abstention

	timer on 39
	use "`votingfolder'/MP_session_analysis", clear
	
	/* Calculate weights */
	
	if `reweight'==1 {
		bys strataSessionPartyFE: egen probHalf = mean(halfOther`Hetero'NeighborRI0)
		bys strataSessionPartyFE: egen probAll = mean(allOther`Hetero'NeighborRI0)
		g noOther`Hetero'NeighborRI0 = halfOther`Hetero'NeighborRI0==0 & allOther`Hetero'NeighborRI0==0 & !mi(halfOther`Hetero'NeighborRI0)
		bys strataSessionPartyFE: egen probNone = mean(noOther`Hetero'NeighborRI0)
		
		g invProb = 1/probHalf if halfOther`Hetero'NeighborRI0==1
		replace invProb = 1/probAll if allOther`Hetero'NeighborRI0==1
		replace invProb = 1/probNone if noOther`Hetero'NeighborRI0==1
	}

	// Define sessions that we will use
	g sampleSession = (!specialSession) & (!shortSession) & !(session_id == 149)
	g sampleSessionLag = !inlist(session_id,115,119,120,124,125,129,130,134,135,137,138,142,143,146,147,148,149)
	g sampleSessionLead = !inlist(session_id,118,119,123,124,128,129,133,134,136,137,141,142,145,146,147,148,149)

	// Three panels: contemporary, lead, lag (placebo)
	forv k = 1/3 { 		// Time period?
		local time: word `k' of "Lag" "" "Lead"	
		
		preserve
			drop if mi(voteDiff4c`time')
	
			forv r = 1/2 {	// Type of voting/cosponsorship measure?
				local measure: word `r' of "absent" "abstain"
				
				areg `measure'`time' fracOther`Hetero'NeighborRI0 if sampleSession`time' `wgt', absorb(strataSessionPartyFE) cluster(MP_id)
				est sto MP`time'`r'
				local obs`time'`r' = e(N)	
				
				// summary stats
				qui su `measure'`time' if e(sample)==1
				local mean`time'`r' = string(r(mean),"%9.2g")
				local sd`time'`r' = string(r(sd),"%9.2g")
				local sd`time'`r'raw = `r(sd)'	
				
				// prepare coefficient, standard error, p-value for export
				local coef_`time'`r'_av = _b[fracOther`Hetero'NeighborRI0]
				local coef_`time'`r'_avs = string(_b[fracOther`Hetero'NeighborRI0],"%9.2g")			
				local se_`time'`r'_av = string(_se[fracOther`Hetero'NeighborRI0],"%9.2g")
				local pval_`time'`r'_av = string(ttail(e(df_r),abs(`coef_`time'`r'_av'/`se_`time'`r'_av'))*2,"%9.2g")	
								
				// diff-party point estimate and 95% confidence interval for figure
				if `k'==2 {
					local mp_`r'_out = `coef_`r'_av'/`sd`r'raw'
					local mpl_`r'_out = (`coef_`r'_av' - invttail(e(df_r),0.025)*`se_`r'_av')/`sd`r'raw'
					local mpu_`r'_out = (`coef_`r'_av' + invttail(e(df_r),0.025)*`se_`r'_av')/`sd`r'raw'
				}		
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme = 0
					forv i=1/`runsRI' {
						
						if `reweight'==1 {
							drop probHalf probAll probNone invProb
							
							bys strataSessionPartyFE: egen probHalf = mean(halfOther`Hetero'NeighborRI`i')
							bys strataSessionPartyFE: egen probAll = mean(allOther`Hetero'NeighborRI`i')
							g noOther`Hetero'NeighborRI`i' = halfOther`Hetero'NeighborRI`i'==0 & ///
									allOther`Hetero'NeighborRI`i'==0 & !mi(halfOther`Hetero'NeighborRI`i')
							bys strataSessionPartyFE: egen probNone = mean(noOther`Hetero'NeighborRI`i')
							
							g invProb = 1/probHalf if halfOther`Hetero'NeighborRI`i'==1
							replace invProb = 1/probAll if allOther`Hetero'NeighborRI`i'==1
							replace invProb = 1/probNone if noOther`Hetero'NeighborRI`i'==1
						}
						
						areg `measure'`time' fracOther`Hetero'NeighborRI`i' if sampleSession`time' `wgt', absorb(strataSessionPartyFE)
						local absCoef = abs(_b[fracOther`Hetero'NeighborRI`i'])
						local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`time'`r'_av')) // + 1 every time see more extreme beta
					}
					local pval_`time'`r'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
				}
				else {
					local pval_`time'`r'_RI = 999
				}
				
				// get stars for all pvals
				foreach y in av RI {
					local pstar_`time'`r'_`y' = "" // reset p-value
					if `pval_`time'`r'_`y''<0.10 & `pval_`time'`r'_`y''>=0.05 local pstar_`time'`r'_`y' = "*"
					if `pval_`time'`r'_`y''<0.05 & `pval_`time'`r'_`y''>=0.01 local pstar_`time'`r'_`y' = "**"
					if `pval_`time'`r'_`y''<0.01 local pstar_`time'`r'_`y' = "***"
				}	
				
				// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
				if `pval_`time'`r'_av'<0.001 local pval_`time'`r'_av = "<0.001" 			
			}
		restore
	}
	
	// Output Main Effects Table
	estout MP1 MP2 MPLead1 MPLead2 MPLag1 MPLag2 using "`tablefolder'//mpVoteAbs`Hetero'A`wgtlab'.tex", style(tex) replace ///
	keep(fracOther`Hetero'NeighborRI0) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(fracOther`Hetero'NeighborRI0 "Proportion Other-`Hetero' Neighbor")

	*local tex "\input{mpVote`Hetero'A`wgtlab'} \vspace{-1.2em} \\"
	local tex "Proportion Other-`Hetero' & `coef_1_avs' & `coef_2_avs' & `coef_Lead1_avs' & `coef_Lead2_avs' & `coef_Lag1_avs' & `coef_Lag2_avs' \\"
	local tex "`tex' Neighbor & (`se_1_av') & (`se_2_av') & (`se_Lead1_av') & (`se_Lead2_av') & (`se_Lag1_av') & (`se_Lag2_av') \\"
	local tex "`tex' & [`pval_1_av']`pstar_1_av' & [`pval_2_av']`pstar_2_av' & [`pval_Lead1_av']`pstar_Lead1_av' & [`pval_Lead2_av']`pstar_Lead2_av' & [`pval_Lag1_av']`pstar_Lag1_av' & [`pval_Lag2_av']`pstar_Lag2_av' \\"
	local tex "`tex' & \{`pval_1_RI'\}`pstar_1_RI' & \{`pval_2_RI'\}`pstar_2_RI' &  \{`pval_Lead1_RI'\}`pstar_Lead1_RI' & \{`pval_Lead2_RI'\}`pstar_Lead2_RI' & \{`pval_Lag1_RI'\}`pstar_Lag1_RI' & \{`pval_Lag2_RI'\}`pstar_Lag2_RI' \\ \\[-5pt] \hline"	
	
	local tex "`tex' Observations & `obs1' & `obs2' & `obsLead1' & `obsLead2' & `obsLag1' & `obsLag2' \\"
	local tex "`tex' Session $\times$ Party $\times$ Strata FE & Y & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `meanLead1' & `meanLead2' & `meanLag1' & `meanLag2' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sdLead1' & `sdLead2' & `sdLag1' & `sdLag2' \\ \toprule" 
	
	local tex "`tex' \multicolumn{7}{p{16cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `wgttabnote'`absenceMP' `abstainMP' `propothMP'"
	local tex "`tex' `mpSEp' `riCurly'"
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDefMP' `plab'}"	
	local tex "`tex' \\ \end{tabular} }"
		
	esttab MP1 MP2 MPLead1 MPLead2 MPLag1 MPLag2 using "`tablefolder'//mpVoteAbs`Hetero'`wgtlab'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("\specialcell{Contemporaneous\\Effect (t)}" "\specialcell{One Year\\Later (t+1)}" ///
			"\specialcell{Previous Year\\(Placebo) (t-1)}", pattern(1 0 1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Absent\\(1)}" "\specialcell{Abstain\\(2)}" ///
	"\specialcell{Absent\\(3)}" "\specialcell{Abstain\\(4)}" ///	
	"\specialcell{Absent\\(5)}" "\specialcell{Abstain\\(6)}")		
	
	timer off 39
	disp "DONE mpAbsentAbstain"
	timer list 39
}
if `dyadVoteBusy'==1 { // Dyadic voting results: busy vs. light voting days
	
	timer on 40
	forv k = 1/3 { 		// Time period?
		local time: word `k' of "Lag" "" "Lead"

		forv r = 1/2 {	// Type of voting/cosponsorship measure?
			local measure: word `r' of "voteSim4c_busy" "voteSim4c_lite"
				
				dis "`measure'`time'"
				use `datamod', clear
								
				keep session_id samevote?c* votediff?c* voteSim?c* *RI* minMP maxMP pairStrata samecoalition sameparty ///
				samegender specialSession shortSession firstSession lastSession leadShortIncomplete	party_id party_id_d	///
				sessionXpairStrataXpartyPair
				
				if `k'==1 {
					drop if firstSession==1 
				}
				else if `k'==3 {
					drop if lastSession==1
					drop if leadShortIncomplete==1
				}
				
				/* Calculate weights */
				if `reweight'==1 {
					bys sessionXpairStrataXpartyPair: egen probTreat = mean(neighborRI0)
					g invProb = 1/probTreat if neighborRI0==1
					replace invProb = 1/(1-probTreat) if neighborRI0==0
				}
				
				/* Neighborhood effect */
				
				// residualize
				qui areg `measure'`time' if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				local numN = e(N)
				local numDummy = e(df_a) 
				predict yresid, residuals
				qui areg neighborRI0 if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				local numN2 = e(N)
				predict xresid, residuals
				assert `numN' == `numN2'
				
				// run regression
				xi: regdyad2 yresid xresid if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d) `wgt', ///
					dyads(minMP maxMP)
				est sto M`time'`r'
				local obs`time'`r' = e(N)
				qui su `measure'`time' if e(sample)==1
				local mean`time'`r' = string(r(mean),"%9.2g")
				local sd`time'`r' = string(r(sd),"%9.2g")
				local sd`time'`r'raw = `r(sd)'
				local numReg = 1
				
				// prepare coefficient, standard error, p-value for export
				local coef_`time'`r'_av = _b[xresid]
				local se_`time'`r'_av = string(_se[xresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1)),"%9.2g")
				local t_df = `numN'-`numReg'-`numDummy'-1
				local pval_`time'`r'_av = string(ttail(`t_df',abs(`coef_`time'`r'_av'/`se_`time'`r'_av'))*2,"%9.2g")					
				drop yresid xresid	
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme = 0
					forv i=1/`runsRI' {
						
						/* Re-calculate weights */
						if `reweight'==1 {
							drop probTreat invProb
							bys sessionXpairStrataXpartyPair: egen probTreat = mean(neighborRI`i')
							g invProb = 1/probTreat if neighborRI`i'==1
							replace invProb = 1/(1-probTreat) if neighborRI`i'==0
						}
						
						areg `measure'`time' neighborRI`i' `wgt', absorb(sessionXpairStrataXpartyPair)
						local absCoef = abs(_b[neighborRI`i'])
						local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`time'`r'_av')) // + 1 every time see more extreme beta
					}
					local pval_`time'`r'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
				}
				else {
					local pval_`time'`r'_RI = 999
				}	
				
				
				/* Re-calculate weights */
				if `reweight'==1 {
					drop probTreat invProb
					bys sessionXpairStrataXpartyPair: egen probTreat = mean(neighborRI0)
					g invProb = 1/probTreat if neighborRI0==1
					replace invProb = 1/(1-probTreat) if neighborRI0==0
				}				
				
				/* Hetero: party ingroup and outgroup effect */
				
				// residualize			
				qui areg `measure'`time' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') ///
					& !mi(`measure'`time') & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair)
				local numN = e(N)
				local numDummy = e(df_a) 
				predict yresid, residuals
				
				qui areg neighborRI0Xsame`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') ///
					& !mi(`measure'`time') & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				local numN2 = e(N)
				predict xinresid, residuals
				assert `numN' == `numN2'
				qui areg neighborRI0Xdiff`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') ///
					& !mi(`measure'`time') & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				predict xoutresid, residuals
				
				// run regression
				xi: regdyad2 yresid xinresid xoutresid if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') ///
					& !mi(`measure'`time') & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d) `wgt', dyads(minMP maxMP)
				est sto Het`time'`r'
				local obsHet`time'`r' = e(N)
				local numReg = 2
				matrix varMatrix = e(V)
				
				// prepare coefficient, standard error, p-value for export					
				local coef_`time'`r'_in = string(_b[xinresid],"%9.2g")
				local coef_`time'`r'_out = string(_b[xoutresid],"%9.2g")
				local se_`time'`r'_in = _se[xinresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
				local se_`time'`r'_out = _se[xoutresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
				local t_df = `numN'-`numReg'-`numDummy'-1
				local pval_`time'`r'_in =  string(ttail(`t_df',abs(`coef_`time'`r'_in'/`se_`time'`r'_in'))*2,"%9.2g")
				local pval_`time'`r'_out =  string(ttail(`t_df',abs(`coef_`time'`r'_out'/`se_`time'`r'_out'))*2,"%9.2g")
				
				// pval calculation for the difference
				local diff = `coef_`time'`r'_in' - `coef_`time'`r'_out'
				local se_`time'`r'_diff = sqrt(varMatrix[1,1]+varMatrix[2,2]-2*varMatrix[2,1])
				local pval_`time'`r'_diff =  string(ttail(`t_df',abs(`diff'/`se_`time'`r'_diff'))*2,"%9.2g")
				drop yresid xinresid xoutresid
				
				// diff-party point estimate and 95% confidence interval for figure
				if `k'==2 {
					local core_`r'_out = `coef_`r'_out'/`sd`r'raw'
					local corel_`r'_out = (`coef_`r'_out' - invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
					local coreu_`r'_out = (`coef_`r'_out' + invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
				}
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme_in = 0
					local Nbeta_extreme_out = 0
					local Nbeta_extreme_diff = 0
					forv i=1/`runsRI' {
						
						/* Re-calculate weights */
						if `reweight'==1 {
							drop probTreat invProb
							bys sessionXpairStrataXpartyPair: egen probTreat = mean(neighborRI`i')
							g invProb = 1/probTreat if neighborRI`i'==1
							replace invProb = 1/(1-probTreat) if neighborRI`i'==0
						}
						
						areg `measure'`time' neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero' `wgt', absorb(sessionXpairStrataXpartyPair)
						local absCoefIn = abs(_b[neighborRI`i'Xsame`hetero'])
						local absCoefOut = abs(_b[neighborRI`i'Xdiff`hetero'])
						local Nbeta_extreme_in = `Nbeta_extreme_in' + (`absCoefIn' >= abs(`coef_`time'`r'_in')) // + 1 every time see more extreme beta
						local Nbeta_extreme_out = `Nbeta_extreme_out' + (`absCoefOut' >= abs(`coef_`time'`r'_out')) 
					
						// for difference between in vs out p-value we need separate regressions, using "modified" outcome
						g `measure'`time'_m = `measure'`time'
						replace `measure'`time'_m = `measure'`time'_m + `coef_`time'`r'_av' if neighborRI`i' == 1 & neighborRI0 == 0
						replace `measure'`time'_m = `measure'`time'_m - `coef_`time'`r'_av' if neighborRI`i' == 0 & neighborRI0 == 1	
						
						// run regression with modified outcome
						areg `measure'`time'_m neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero' `wgt', absorb(sessionXpairStrataXpartyPair)
						local absCoefDiff = abs(_b[neighborRI`i'Xsame`hetero']-_b[neighborRI`i'Xdiff`hetero'])
						local Nbeta_extreme_diff = `Nbeta_extreme_diff' + (`absCoefDiff' >= abs(`diff')) 
						drop `measure'`time'_m
					}
					local pval_`time'`r'_RIin = string(`Nbeta_extreme_in'/`runsRI',"%9.2g")
					local pval_`time'`r'_RIout = string(`Nbeta_extreme_out'/`runsRI',"%9.2g")	
					local pval_`time'`r'_RIdiff = string(`Nbeta_extreme_diff'/`runsRI',"%9.2g")	
				}
				else {
					local pval_`time'`r'_RIin = 999
					local pval_`time'`r'_RIout = 999	
					local pval_`time'`r'_RIdiff = 999										
				}					
				
				// get stars for all pvals
				foreach y in av in out diff RI RIin RIout RIdiff {
					local pstar_`time'`r'_`y' = "" // reset p-value
					if `pval_`time'`r'_`y''<0.10 & `pval_`time'`r'_`y''>=0.05 local pstar_`time'`r'_`y' = "*"
					if `pval_`time'`r'_`y''<0.05 & `pval_`time'`r'_`y''>=0.01 local pstar_`time'`r'_`y' = "**"
					if `pval_`time'`r'_`y''<0.01 local pstar_`time'`r'_`y' = "***"
				}
				
				// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
				foreach y in av in out diff {
					if `pval_`time'`r'_`y''<0.001 local pval_`time'`r'_`y' = "<0.001" 
				}					
		}
	}
		
	// Output Main Effects Table (appendix?)
	estout M1 M2 MLead1 MLead2 MLag1 MLag2 using "`tablefolder'//pairVoteBusyA`wgtlab'.tex", style(tex) replace ///
	keep(xresid) cells(b(nostar fmt(%9.2g))) ///
	nolabel collabels(none) mlabels(none) varlabels(xresid "Neighbor")

	local tex "\input{pairVoteBusyA`wgtlab'} \vspace{-1.2em} \\"
	local tex "`tex' & (`se_1_av') & (`se_2_av') & (`se_Lead1_av') & (`se_Lead2_av') & (`se_Lag1_av') & (`se_Lag2_av') \\"
	local tex "`tex' & [`pval_1_av']`pstar_1_av' & [`pval_2_av']`pstar_2_av' & [`pval_Lead1_av']`pstar_Lead1_av' & [`pval_Lead2_av']`pstar_Lead2_av' & [`pval_Lag1_av']`pstar_Lag1_av' & [`pval_Lag2_av']`pstar_Lag2_av' \\"
	local tex "`tex' & \{`pval_1_RI'\}`pstar_1_RI' & \{`pval_2_RI'\}`pstar_2_RI' &  \{`pval_Lead1_RI'\}`pstar_Lead1_RI' & \{`pval_Lead2_RI'\}`pstar_Lead2_RI' & \{`pval_Lag1_RI'\}`pstar_Lag1_RI' & \{`pval_Lag2_RI'\}`pstar_Lag2_RI' \\ \\[-5pt] \hline"	
	
	local tex "`tex' Observations & `obs1' & `obs2' & `obsLead1' & `obsLead2' & `obsLag1' & `obsLag2' \\"
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `meanLead1' & `meanLead2' & `meanLag1' & `meanLag2' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sdLead1' & `sdLead2' & `sdLag1' & `sdLag2' \\ \toprule" 
	
	local tex "`tex' \multicolumn{7}{p{19.3cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `wgttabnote'`similarity'"
	local tex "`tex' The Busy measure considers only votes on days with at least 50 votes."
	local tex "`tex' The Light measure considers the remaining votes."
	local tex "`tex' `neighbor'"
	local tex "`tex' `dyadSEp' `riCurly'" 
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `plab'}"	
	local tex "`tex' \\ \end{tabular} }"
	
	esttab M1 M2 MLead1 MLead2 MLag1 MLag2 using "`tablefolder'//pairVoteBusy`wgtlab'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("\specialcell{Contemporaneous\\Effect (t)}" "\specialcell{One Year\\Later (t+1)}" ///
			"\specialcell{Previous Year\\(Placebo) (t-1)}", pattern(1 0 1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Similarity\\Busy\\(1)}" "\specialcell{Similarity\\Light\\(2)}" ///
	"\specialcell{Similarity\\Busy\\(3)}" "\specialcell{Similarity\\Light\\(4)}" ///	
	"\specialcell{Similarity\\Busy\\(5)}" "\specialcell{Similarity\\Light\\(6)}")	

	// Output Heterogeneous Effects Table
	local tex "Neighbor $\times$ Different `Hetero' & `coef_1_out' & `coef_2_out' & `coef_Lead1_out' & `coef_Lead2_out' & `coef_Lag1_out' & `coef_Lag2_out' \\"
	local tex "`tex' `bipart' & [`pval_1_out']`pstar_1_out' & [`pval_2_out']`pstar_2_out' & [`pval_Lead1_out']`pstar_Lead1_out' & [`pval_Lead2_out']`pstar_Lead2_out' & [`pval_Lag1_out']`pstar_Lag1_out' & [`pval_Lag2_out']`pstar_Lag2_out' \\"
	local tex "`tex' & \{`pval_1_RIout'\}`pstar_1_RIout' & \{`pval_2_RIout'\}`pstar_2_RIout' & \{`pval_Lead1_RIout'\}`pstar_Lead1_RIout' & \{`pval_Lead2_RIout'\}`pstar_Lead2_RIout' & \{`pval_Lag1_RIout'\}`pstar_Lag1_RIout' & \{`pval_Lag2_RIout'\}`pstar_Lag2_RIout' \\ \\[-5pt]"		
	
	local tex "`tex' Neighbor $\times$ Same `Hetero' & `coef_1_in' & `coef_2_in' & `coef_Lead1_in' & `coef_Lead2_in'  & `coef_Lag1_in' & `coef_Lag2_in' \\"
	local tex "`tex'  & [`pval_1_in']`pstar_1_in' & [`pval_2_in']`pstar_2_in' & [`pval_Lead1_in']`pstar_Lead1_in' & [`pval_Lead2_in']`pstar_Lead2_in' & [`pval_Lag1_in']`pstar_Lag1_in' & [`pval_Lag2_in']`pstar_Lag2_in' \\"
	local tex "`tex' & \{`pval_1_RIin'\}`pstar_1_RIin' & \{`pval_2_RIin'\}`pstar_2_RIin' & \{`pval_Lead1_RIin'\}`pstar_Lead1_RIin' & \{`pval_Lead2_RIin'\}`pstar_Lead2_RIin' & \{`pval_Lag1_RIin'\}`pstar_Lag1_RIin' & \{`pval_Lag2_RIin'\}`pstar_Lag2_RIin' \\ \\[-5pt]"		
		
	local tex "`tex' Same = Different & [`pval_1_diff']`pstar_1_diff' & [`pval_2_diff']`pstar_2_diff' & [`pval_Lead1_diff']`pstar_Lead1_diff' & [`pval_Lead2_diff']`pstar_Lead2_diff' & [`pval_Lag1_diff']`pstar_Lag1_diff' & [`pval_Lag2_diff']`pstar_Lag2_diff' \\"
	local tex "`tex' & \{`pval_1_RIdiff'\}`pstar_1_RIdiff' & \{`pval_2_RIdiff'\}`pstar_2_RIdiff' & \{`pval_Lead1_RIdiff'\}`pstar_Lead1_RIdiff' & \{`pval_Lead2_RIdiff'\}`pstar_Lead2_RIdiff' & \{`pval_Lag1_RIdiff'\}`pstar_Lag1_RIdiff' & \{`pval_Lag2_RIdiff'\}`pstar_Lag2_RIdiff' \\ \\[-5pt] \hline"				
	
	local tex "`tex' Observations & `obsHet1' & `obsHet2' & `obsHetLead1' & `obsHetLead2' & `obsHetLag1' & `obsHetLag2' \\" 
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `meanLead1' & `meanLead2' & `meanLag1' & `meanLag2' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sdLead1' & `sdLead2' & `sdLag1' & `sdLag2' \\ \toprule" 
	
	local tex "`tex' \multicolumn{7}{p{19.5cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `wgttabnote'`similarity'"
	local tex "`tex' The Busy measure considers only votes on days with at least 50 votes."
	local tex "`tex' The Light measure considers the remaining votes."
	local tex "`tex' `neighbor' `samehet'"
	local tex "`tex' `dyadSquare' `riCurly'"
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `plab'}"
	local tex "`tex' \\ \end{tabular} }"
	
	esttab Het1 Het2 HetLead1 HetLead2 HetLag1 HetLag2 using "`tablefolder'//pairVoteBusy`Hetero'`wgtlab'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("\specialcell{Contemporaneous\\Effect (t)}" "\specialcell{One Year\\Later (t+1)}" ///
			"\specialcell{Previous Year\\(Placebo) (t-1)}", pattern(1 0 1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Similarity\\Busy\\(1)}" "\specialcell{Similarity\\Light\\(2)}" ///
	"\specialcell{Similarity\\Busy\\(3)}" "\specialcell{Similarity\\Light\\(4)}" ///	
	"\specialcell{Similarity\\Busy\\(5)}" "\specialcell{Similarity\\Light\\(6)}")	
	
	timer off 40
	disp "DONE dyadVoteBusy"
	timer list 40
}
if `dyadVoteGenderExper'==1 { // Dyadic voting results, for heterogeneity by gender and diff. in experience
	
	timer on 41
	forv k = 1/3 { 		// Time period?
		local time: word `k' of "Lag" "" "Lead"

		forv r = 1/2 {	// Type of voting/cosponsorship measure?
			local measure: word `r' of "samevote4c" "voteSim4c"
				
				dis "`measure'`time'"
				use `datamod', clear
								
				keep session_id samevote?c* votediff?c* voteSim?c* *RI* minMP maxMP pairStrata samecoalition sameparty ///
				samegender specialSession shortSession firstSession lastSession leadShortIncomplete	party_id party_id_d	///
				sessionXpairStrataXpartyPair sameexperience
				
				if `k'==1 {
					drop if firstSession==1 
				}
				else if `k'==3 {
					drop if lastSession==1
					drop if leadShortIncomplete==1
				}
				
				
				/* Calculate weights */
				if `reweight'==1 {
					bys sessionXpairStrataXpartyPair: egen probTreat = mean(neighborRI0)
					g invProb = 1/probTreat if neighborRI0==1
					replace invProb = 1/(1-probTreat) if neighborRI0==0
				}
				
				/* Neighborhood effect */
				
				// residualize
				qui areg `measure'`time' if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				local numN = e(N)
				local numDummy = e(df_a) 
				predict yresid, residuals
				
				qui areg neighborRI0 if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				local numN2 = e(N)
				predict xresid, residuals
				assert `numN' == `numN2'
				
				qui areg same`hetero' if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				predict sameavresid, residuals				
				
				// run regression
				xi: regdyad2 yresid xresid sameavresid if !mi(`measure'`time') & !mi(neighborRI0) & !mi(pairStrata) ///
						& !mi(party_id) & !mi(party_id_d) `wgt', dyads(minMP maxMP)
				est sto M`time'`r'
				local obs`time'`r' = e(N)
				qui su `measure'`time' if e(sample)==1
				local mean`time'`r' = string(r(mean),"%9.2g")
				local sd`time'`r' = string(r(sd),"%9.2g")
				local sd`time'`r'raw = `r(sd)'
				local numReg = 2
				
				// prepare coefficient, standard error, p-value for export
				local coef_`time'`r'_av = _b[xresid]
				local se_`time'`r'_av = string(_se[xresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1)),"%9.2g")
				local t_df = `numN'-`numReg'-`numDummy'-1
				local pval_`time'`r'_av = string(ttail(`t_df',abs(`coef_`time'`r'_av'/`se_`time'`r'_av'))*2,"%9.2g")					
				drop yresid xresid	
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme = 0
					forv i=1/`runsRI' {
						
						/* Re-calculate weights */
						if `reweight'==1 {
							drop probTreat invProb
							bys sessionXpairStrataXpartyPair: egen probTreat = mean(neighborRI`i')
							g invProb = 1/probTreat if neighborRI`i'==1
							replace invProb = 1/(1-probTreat) if neighborRI`i'==0
						}
						
						areg `measure'`time' neighborRI`i' `wgt', absorb(sessionXpairStrataXpartyPair)
						local absCoef = abs(_b[neighborRI`i'])
						local Nbeta_extreme = `Nbeta_extreme' + (`absCoef' >= abs(`coef_`time'`r'_av')) // + 1 every time see more extreme beta
					}
					local pval_`time'`r'_RI = string(`Nbeta_extreme'/`runsRI',"%9.2g")
				}
				else {
					local pval_`time'`r'_RI = 999
				}	
				
				
				/* Re-calculate weights */
				if `reweight'==1 {
					drop probTreat invProb
					bys sessionXpairStrataXpartyPair: egen probTreat = mean(neighborRI0)
					g invProb = 1/probTreat if neighborRI0==1
					replace invProb = 1/(1-probTreat) if neighborRI0==0
				}				
				
				/* Hetero: party ingroup and outgroup effect */
				
				// residualize			
				qui areg `measure'`time' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') ///
					& !mi(`measure'`time') & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair)
				local numN = e(N)
				local numDummy = e(df_a) 
				predict yresid, residuals
				
				qui areg neighborRI0Xsame`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') ///
					& !mi(`measure'`time') & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				local numN2 = e(N)
				predict xinresid, residuals
				assert `numN' == `numN2'
				
				qui areg neighborRI0Xdiff`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') ///
					& !mi(`measure'`time') & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				predict xoutresid, residuals
				
				qui areg same`hetero' if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') ///
					& !mi(`measure'`time') & !mi(pairStrata) `wgt', absorb(sessionXpairStrataXpartyPair) 
				predict sameresid, residuals				
				
				// run regression
				xi: regdyad2 yresid xinresid xoutresid sameresid if !mi(neighborRI0Xsame`hetero') & !mi(neighborRI0Xdiff`hetero') ///
					& !mi(`measure'`time') & !mi(pairStrata) & !mi(party_id) & !mi(party_id_d) `wgt', dyads(minMP maxMP)
				est sto Het`time'`r'
				local obsHet`time'`r' = e(N)
				local numReg = 3
				matrix varMatrix = e(V)
				
				// prepare coefficient, standard error, p-value for export					
				local coef_`time'`r'_in = string(_b[xinresid],"%9.2g")
				local coef_`time'`r'_out = string(_b[xoutresid],"%9.2g")
				local se_`time'`r'_in = _se[xinresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
				local se_`time'`r'_out = _se[xoutresid]*sqrt((`numN'-`numReg'-1)/(`numN'-`numReg'-`numDummy'-1))
				local t_df = `numN'-`numReg'-`numDummy'-1
				local pval_`time'`r'_in =  string(ttail(`t_df',abs(`coef_`time'`r'_in'/`se_`time'`r'_in'))*2,"%9.2g")
				local pval_`time'`r'_out =  string(ttail(`t_df',abs(`coef_`time'`r'_out'/`se_`time'`r'_out'))*2,"%9.2g")
				
				// pval calculation for the difference
				local diff = `coef_`time'`r'_in' - `coef_`time'`r'_out'
				local se_`time'`r'_diff = sqrt(varMatrix[1,1]+varMatrix[2,2]-2*varMatrix[2,1])
				local pval_`time'`r'_diff =  string(ttail(`t_df',abs(`diff'/`se_`time'`r'_diff'))*2,"%9.2g")
				drop yresid xinresid xoutresid
				
				// diff-party point estimate and 95% confidence interval for figure
				if `k'==2 {
					local core_`r'_out = `coef_`r'_out'/`sd`r'raw'
					local corel_`r'_out = (`coef_`r'_out' - invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
					local coreu_`r'_out = (`coef_`r'_out' + invttail(`t_df',0.025)*`se_`r'_out')/`sd`r'raw'
				}
				
				// randomization inference p-values
				if `RI'==1 {
					local Nbeta_extreme_in = 0
					local Nbeta_extreme_out = 0
					local Nbeta_extreme_diff = 0
					forv i=1/`runsRI' {
						
						/* Re-calculate weights */
						if `reweight'==1 {
							drop probTreat invProb
							bys sessionXpairStrataXpartyPair: egen probTreat = mean(neighborRI`i')
							g invProb = 1/probTreat if neighborRI`i'==1
							replace invProb = 1/(1-probTreat) if neighborRI`i'==0
						}
						
						areg `measure'`time' neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero' same`hetero' `wgt', absorb(sessionXpairStrataXpartyPair)
						local absCoefIn = abs(_b[neighborRI`i'Xsame`hetero'])
						local absCoefOut = abs(_b[neighborRI`i'Xdiff`hetero'])
						local Nbeta_extreme_in = `Nbeta_extreme_in' + (`absCoefIn' >= abs(`coef_`time'`r'_in')) // + 1 every time see more extreme beta
						local Nbeta_extreme_out = `Nbeta_extreme_out' + (`absCoefOut' >= abs(`coef_`time'`r'_out')) 
					
						// for difference between in vs out p-value we need separate regressions, using "modified" outcome
						g `measure'`time'_m = `measure'`time'
						replace `measure'`time'_m = `measure'`time'_m + `coef_`time'`r'_av' if neighborRI`i' == 1 & neighborRI0 == 0
						replace `measure'`time'_m = `measure'`time'_m - `coef_`time'`r'_av' if neighborRI`i' == 0 & neighborRI0 == 1	
						
						// run regression with modified outcome
						areg `measure'`time'_m neighborRI`i'Xsame`hetero' neighborRI`i'Xdiff`hetero' same`hetero' `wgt', absorb(sessionXpairStrataXpartyPair)
						local absCoefDiff = abs(_b[neighborRI`i'Xsame`hetero']-_b[neighborRI`i'Xdiff`hetero'])
						local Nbeta_extreme_diff = `Nbeta_extreme_diff' + (`absCoefDiff' >= abs(`diff')) 
						drop `measure'`time'_m
					}
					local pval_`time'`r'_RIin = string(`Nbeta_extreme_in'/`runsRI',"%9.2g")
					local pval_`time'`r'_RIout = string(`Nbeta_extreme_out'/`runsRI',"%9.2g")	
					local pval_`time'`r'_RIdiff = string(`Nbeta_extreme_diff'/`runsRI',"%9.2g")	
				}
				else {
					local pval_`time'`r'_RIin = 999
					local pval_`time'`r'_RIout = 999	
					local pval_`time'`r'_RIdiff = 999										
				}					
				
				// get stars for all pvals
				foreach y in av in out diff RI RIin RIout RIdiff {
					local pstar_`time'`r'_`y' = "" // reset p-value
					if `pval_`time'`r'_`y''<0.10 & `pval_`time'`r'_`y''>=0.05 local pstar_`time'`r'_`y' = "*"
					if `pval_`time'`r'_`y''<0.05 & `pval_`time'`r'_`y''>=0.01 local pstar_`time'`r'_`y' = "**"
					if `pval_`time'`r'_`y''<0.01 local pstar_`time'`r'_`y' = "***"
				}
				
				// replace to <0.001 if tiny (not for RI, since RI always multiple of 0.005)
				foreach y in av in out diff {
					if `pval_`time'`r'_`y''<0.001 local pval_`time'`r'_`y' = "<0.001" 
				}					
		}
	}
	
	// Output Heterogeneous Effects Table
	local tex "Neighbor $\times$ Different `Hetero' & `coef_1_out' & `coef_2_out' & `coef_Lead1_out' & `coef_Lead2_out' & `coef_Lag1_out' & `coef_Lag2_out' \\"
	local tex "`tex' `bipart' & [`pval_1_out']`pstar_1_out' & [`pval_2_out']`pstar_2_out' & [`pval_Lead1_out']`pstar_Lead1_out' & [`pval_Lead2_out']`pstar_Lead2_out' & [`pval_Lag1_out']`pstar_Lag1_out' & [`pval_Lag2_out']`pstar_Lag2_out' \\"
	local tex "`tex' & \{`pval_1_RIout'\}`pstar_1_RIout' & \{`pval_2_RIout'\}`pstar_2_RIout' & \{`pval_Lead1_RIout'\}`pstar_Lead1_RIout' & \{`pval_Lead2_RIout'\}`pstar_Lead2_RIout' & \{`pval_Lag1_RIout'\}`pstar_Lag1_RIout' & \{`pval_Lag2_RIout'\}`pstar_Lag2_RIout' \\ \\[-5pt]"		
	
	local tex "`tex' Neighbor $\times$ Same `Hetero' & `coef_1_in' & `coef_2_in' & `coef_Lead1_in' & `coef_Lead2_in'  & `coef_Lag1_in' & `coef_Lag2_in' \\"
	local tex "`tex'  & [`pval_1_in']`pstar_1_in' & [`pval_2_in']`pstar_2_in' & [`pval_Lead1_in']`pstar_Lead1_in' & [`pval_Lead2_in']`pstar_Lead2_in' & [`pval_Lag1_in']`pstar_Lag1_in' & [`pval_Lag2_in']`pstar_Lag2_in' \\"
	local tex "`tex' & \{`pval_1_RIin'\}`pstar_1_RIin' & \{`pval_2_RIin'\}`pstar_2_RIin' & \{`pval_Lead1_RIin'\}`pstar_Lead1_RIin' & \{`pval_Lead2_RIin'\}`pstar_Lead2_RIin' & \{`pval_Lag1_RIin'\}`pstar_Lag1_RIin' & \{`pval_Lag2_RIin'\}`pstar_Lag2_RIin' \\ \\[-5pt]"		
		
	local tex "`tex' Same = Different & [`pval_1_diff']`pstar_1_diff' & [`pval_2_diff']`pstar_2_diff' & [`pval_Lead1_diff']`pstar_Lead1_diff' & [`pval_Lead2_diff']`pstar_Lead2_diff' & [`pval_Lag1_diff']`pstar_Lag1_diff' & [`pval_Lag2_diff']`pstar_Lag2_diff' \\"
	local tex "`tex' & \{`pval_1_RIdiff'\}`pstar_1_RIdiff' & \{`pval_2_RIdiff'\}`pstar_2_RIdiff' & \{`pval_Lead1_RIdiff'\}`pstar_Lead1_RIdiff' & \{`pval_Lead2_RIdiff'\}`pstar_Lead2_RIdiff' & \{`pval_Lag1_RIdiff'\}`pstar_Lag1_RIdiff' & \{`pval_Lag2_RIdiff'\}`pstar_Lag2_RIdiff' \\ \\[-5pt] \hline"				
	
	local tex "`tex' Observations & `obsHet1' & `obsHet2' & `obsHetLead1' & `obsHetLead2' & `obsHetLag1' & `obsHetLag2' \\" 
	local tex "`tex' Same `Hetero' Dummy & Y & Y & Y & Y & Y & Y \\"	
	local tex "`tex' Session $\times$ Party Pair $\times$ Strata FE & Y & Y & Y & Y & Y & Y \\"
	local tex "`tex' Outcome Mean & `mean1' & `mean2' & `meanLead1' & `meanLead2' & `meanLag1' & `meanLag2' \\"  
	local tex "`tex' Outcome S.d. & `sd1' & `sd2' & `sdLead1' & `sdLead2' & `sdLag1' & `sdLag2' \\ \toprule" 
	
	local tex "`tex' \multicolumn{7}{p{20cm}}{\footnotesize \textit{Notes:}"
	local tex "`tex' `wgttabnote'`compliance' `similarity'"
	local tex "`tex' `neighbor'"
	
	if "`hetero'"=="gender" {
		local tex "`tex' Same Gender is equal to one if both MPs in the pair have the same gender. Different Gender is equal to one minus Same Gender."
	}
	else if "`hetero'"=="experience" {
		local tex "`tex' Same Experience is equal to one if the difference in political experience between the two MPs in the pair is five sessions or less."
		local tex "`tex' Different Experience is equal to one if the difference in experience is more than five sessions."
	}
	
	local tex "`tex' `dyadSquare' `riCurly'"
	local tex "`tex' `specShortDrop' `leadLagDrop'" 
	local tex "`tex' `strataDef' `plab'}"
	local tex "`tex' \\ \end{tabular} }"
	
	esttab Het1 Het2 HetLead1 HetLead2 HetLag1 HetLag2 using "`tablefolder'//pairVote`Hetero'`wgtlab'.tex", style(tex) booktabs replace ///
	d(*) nolabel collabels(none) noobs postfoot("`tex'") nonum ///
	mgroups("\specialcell{Contemporaneous\\Effect (t)}" "\specialcell{One Year\\Later (t+1)}" ///
			"\specialcell{Previous Year\\(Placebo) (t-1)}", pattern(1 0 1 0 1 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("\specialcell{Compliance\\(1)}" "\specialcell{Similarity\\(2)}" ///
	"\specialcell{Compliance\\(3)}" "\specialcell{Similarity\\(4)}" ///	
	"\specialcell{Compliance\\(5)}" "\specialcell{Similarity\\(6)}")	
	
	timer off 42
	disp "DONE dyadVoteGenderExper"
	timer list 42
}
timer off 99
timer list 99
beep
